<html>
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"/>
  <title>
   SQL Server 致程序员：容易忽略的错误  | 数螺 | NAUT IDEA
  </title>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet"/>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
  <style type="text/css">
   #xmain img {
                  max-width: 100%;
                  display: block;
                  margin-top: 10px;
                  margin-bottom: 10px;
                }

                #xmain p {
                    line-height:150%;
                    font-size: 16px;
                    margin-top: 20px;
                }

                #xmain h2 {
                    font-size: 24px;
                }

                #xmain h3 {
                    font-size: 20px;
                }

                #xmain h4 {
                    font-size: 18px;
                }


                .header {
	           background-color: #0099ff;
	           color: #ffffff;
	           margin-bottom: 20px;
	        }

	        .header p {
                  margin: 0px;
                  padding: 10px 0;
                  display: inline-block;  
                  vertical-align: middle;
                  font-size: 16px;
               }

               .header a {
                 color: white;
               }

              .header img {
                 height: 25px;
              }
  </style>
  <script src="http://cdn.bootcss.com/jquery/3.0.0/jquery.min.js">
  </script>
  <script src="http://nautstatic-10007657.file.myqcloud.com/static/css/readability.min.js" type="text/javascript">
  </script>
  <script type="text/javascript">
   $(document).ready(function() {
                 var loc = document.location;
                 var uri = {
                  spec: "http://dataunion.org/17664.html",
                  host: "http://dataunion.org",
                  prePath: "http://dataunion.org",
                  scheme: "http",
                  pathBase: "http://dataunion.org/"
                 };
    
                 var documentClone = document.cloneNode(true);
                 var article = new Readability(uri, documentClone).parse();
     
                 document.getElementById("xmain").innerHTML = article.content;
                });
  </script>
  <!-- 1466454870: Accept with keywords: (title(0.0):程序员,社区,数盟,错误, topn(0.2):触发器,数盟,程序员,查找,应用程序,条件,主表,过程,结果,数据库,计划,基础架构,谓词,文章,类型,日志,排序,数据,事务,记录,性能,消耗,问题,语句,错误,新闻,测试数据,字段,时候,内存).-->
 </head>
 <body onload="">
  <div class="header">
   <div class="container">
    <div class="row">
     <div class="col-xs-6 col-sm-6 text-left">
      <a href="/databee">
       <img src="http://nautidea-10007657.cos.myqcloud.com/logo_white.png"/>
      </a>
      <a href="/databee">
       <p>
        数螺
       </p>
      </a>
     </div>
     <div class="hidden-xs col-sm-6 text-right">
      <p>
       致力于数据科学的推广和知识传播
      </p>
     </div>
    </div>
   </div>
  </div>
  <div class="container text-center">
   <h1>
    SQL Server 致程序员：容易忽略的错误
   </h1>
  </div>
  <div class="container" id="xmain">
   ﻿﻿
   <title>
    SQL Server 致程序员：容易忽略的错误 | 数盟社区
   </title>
   <!-- All in One SEO Pack 2.2.7.6.2 by Michael Torbert of Semper Fi Web Design[32,70] -->
   <!-- /all in one seo pack -->
   <!--
<div align="center">
<a href="http://strata.oreilly.com.cn/hadoop-big-data-cn?cmp=mp-data-confreg-home-stcn16_dataunion_pc" target="_blank"><img src="http://dataunion.org/wp-content/uploads/2016/05/stratabj.jpg"/ ></a>
</div>
-->
   <header id="header-web">
    <div class="header-main">
     <hgroup class="logo">
      <h1>
       <a href="http://dataunion.org/" rel="home" title="数盟社区">
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/logo.png"/>
       </a>
      </h1>
     </hgroup>
     <!--logo-->
     <nav class="header-nav">
      <ul class="menu" id="menu-%e4%b8%bb%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-71" id="menu-item-71">
        <a href="http://dataunion.org/category/events" title="events">
         活动
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22457" id="menu-item-22457">
          <a href="http://dataunion.org/2016timeline">
           2016档期
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22459" id="menu-item-22459">
          <a href="http://dataunion.org/category/parterc">
           合作会议
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor menu-item-has-children menu-item-20869" id="menu-item-20869">
        <a href="http://dataunion.org/category/tech" title="articles">
         文章
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-20867" id="menu-item-20867">
          <a href="http://dataunion.org/category/tech/base" title="base">
           基础架构
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3302" id="menu-item-3302">
          <a href="http://dataunion.org/category/tech/ai" title="ai">
           人工智能
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3303" id="menu-item-3303">
          <a href="http://dataunion.org/category/tech/analysis" title="analysis">
           数据分析
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21920" id="menu-item-21920">
          <a href="http://dataunion.org/category/tech/dm">
           数据挖掘
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3314" id="menu-item-3314">
          <a href="http://dataunion.org/category/tech/viz" title="viz">
           可视化
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3305" id="menu-item-3305">
          <a href="http://dataunion.org/category/tech/devl" title="devl">
           编程语言
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-20876" id="menu-item-20876">
        <a href="http://dataunion.org/category/industry">
         行业
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-16328" id="menu-item-16328">
          <a href="http://dataunion.org/category/industry/case" title="case">
           行业应用
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2112" id="menu-item-2112">
          <a href="http://dataunion.org/category/industry/demo" title="demo">
           Demo展示
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21562" id="menu-item-21562">
          <a href="http://dataunion.org/category/industry/news">
           行业资讯
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-311" id="menu-item-311">
        <a href="http://dataunion.org/category/sources" title="sources">
         资源
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20870" id="menu-item-20870">
        <a href="http://dataunion.org/category/books" title="book">
         图书
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21363" id="menu-item-21363">
        <a href="http://dataunion.org/category/training">
         课程
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-21853" id="menu-item-21853">
        <a href="http://dataunion.org/category/jobs">
         职位
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22050" id="menu-item-22050">
          <a href="http://dataunion.org/category/career">
           职业规划
          </a>
         </li>
        </ul>
       </li>
      </ul>
     </nav>
     <!--header-nav-->
    </div>
   </header>
   <!--header-web-->
   <div id="main">
    <div id="soutab">
     <form action="http://dataunion.org/" class="search" method="get">
     </form>
    </div>
    <div id="container">
     <nav id="mbx">
      当前位置：
      <a href="http://dataunion.org">
       首页
      </a>
      &gt;
      <a href="http://dataunion.org/category/tech">
       文章
      </a>
      &gt;
      <a href="http://dataunion.org/category/tech/base">
       基础架构
      </a>
      &gt;  正文
     </nav>
     <!--mbx-->
     <article class="content">
      <header align="centre" class="contenttitle">
       <div class="mscc">
        <h1 class="mscctitle">
         <a href="http://dataunion.org/17664.html">
          SQL Server 致程序员：容易忽略的错误
         </a>
        </h1>
        <address class="msccaddress ">
         <em>
          1,681 次阅读 -
         </em>
         <a href="http://dataunion.org/category/tech/base" rel="category tag">
          基础架构
         </a>
        </address>
       </div>
      </header>
      <div class="content-text">
       <p>
        <strong>
         概述
        </strong>
       </p>
       <p>
        因为每天需要审核程序员发布的SQL语句，所以收集了一些程序员的一些常见问题，还有一些平时收集的其它一些问题，这也是很多人容易忽视的问题，在以后收集到的问题会补充在文章末尾，欢迎关注，由于收集的问题很多是针对于生产数据，测试且数据量比较大，这里就不把数据共享出来了，大家理解意思就行。
       </p>
       <div>
       </div>
       <p>
        <strong>
         步骤
        </strong>
       </p>
       <h3>
        <strong>
         大小写
        </strong>
       </h3>
       <p align="left">
        大写T-SQL 语言的所有关键字都使用大写，规范要求。
       </p>
       <h3>
        <strong>
         使用“;”
        </strong>
       </h3>
       <p align="left">
        使用“;”作为 Transact-SQL 语句终止符。虽然分号不是必需的，但使用它是一种好的习惯,对于合并操作MERGE语句的末尾就必须要加上“;”
       </p>
       <p align="left">
        （cte表表达式除外）
       </p>
       <h3>
        <strong>
         数据类型
        </strong>
       </h3>
       <p>
        避免使用ntext、text 和 image 数据类型，用 nvarchar(max)、varchar(max) 和 varbinary(max)替代
       </p>
       <p>
        后续版本会取消ntext、text 和 image 该三种类型
       </p>
       <p>
       </p>
       <h3>
        <strong>
         查询条件不要使用计算列
        </strong>
       </h3>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43548346874902" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           例如year(createdate)=2014,使用createdate&amp;gt;=’ 20140101’ and createdate&amp;lt;=’ 20141231’来取代。
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43548346874902-1">
                1
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43548346874902-1">
                例如
                <span class="crayon-e">
                 year
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 createdate
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 2014
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                使用
                <span class="crayon-v">
                 createdate
                </span>
                <span class="crayon-o">
                 &amp;
                </span>
                <span class="crayon-v">
                 gt
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-o">
                 =
                </span>
                ’
                <span class="crayon-h">
                </span>
                <span class="crayon-cn">
                 20140101
                </span>
                ’
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 and
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 createdate
                </span>
                <span class="crayon-o">
                 &amp;
                </span>
                <span class="crayon-v">
                 lt
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-o">
                 =
                </span>
                ’
                <span class="crayon-h">
                </span>
                <span class="crayon-cn">
                 20141231
                </span>
                ’来取代。
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0013 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code">
         <div class="cnblogs_code_toolbar">
          <span class="cnblogs_code_copy">
           <p title="复制代码">
            <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
           </p>
          </span>
         </div>
         <p>
         </p>
         <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
         <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43557636513993" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
          <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <span class="crayon-title">
           </span>
           <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
            <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-wrap-button" title="切换自动换行">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-expand-button" title="点击展开代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-copy-button" title="复制代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
           </div>
          </div>
          <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
          </div>
          <div class="crayon-plain-wrap">
           <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
            IF OBJECT_ID('News','U') IS NOT NULL DROP TABLE News
GO
CREATE TABLE News
(ID INT NOT NULL PRIMARY KEY IDENTITY(1,1),
NAME NVARCHAR(100) NOT NULL,
Createdate DATETIME NOT NULL
)
GO
CREATE NONCLUSTERED INDEX [IX1_News] ON [dbo].[News] 
(
    [Createdate] ASC
)
INCLUDE ( [NAME]) WITH (STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO

GO
INSERT INTO News(NAME,Createdate) 
VALUES( '新闻','2014-08-20 00:00:00'),( '新闻','2014-08-20 00:00:00'),( '新闻','2014-08-20 00:00:00'),( '新闻','2014-08-20 00:00:00')
           </textarea>
          </div>
          <div class="crayon-main" style="">
           <table class="crayon-table">
            <tbody>
             <tr class="crayon-row">
              <td class="crayon-nums " data-settings="show">
               <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
                <div class="crayon-num" data-line="crayon-5768534d43557636513993-1">
                 1
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43557636513993-2">
                 2
                </div>
                <div class="crayon-num" data-line="crayon-5768534d43557636513993-3">
                 3
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43557636513993-4">
                 4
                </div>
                <div class="crayon-num" data-line="crayon-5768534d43557636513993-5">
                 5
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43557636513993-6">
                 6
                </div>
                <div class="crayon-num" data-line="crayon-5768534d43557636513993-7">
                 7
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43557636513993-8">
                 8
                </div>
                <div class="crayon-num" data-line="crayon-5768534d43557636513993-9">
                 9
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43557636513993-10">
                 10
                </div>
                <div class="crayon-num" data-line="crayon-5768534d43557636513993-11">
                 11
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43557636513993-12">
                 12
                </div>
                <div class="crayon-num" data-line="crayon-5768534d43557636513993-13">
                 13
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43557636513993-14">
                 14
                </div>
                <div class="crayon-num" data-line="crayon-5768534d43557636513993-15">
                 15
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43557636513993-16">
                 16
                </div>
                <div class="crayon-num" data-line="crayon-5768534d43557636513993-17">
                 17
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43557636513993-18">
                 18
                </div>
               </div>
              </td>
              <td class="crayon-code">
               <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
                <div class="crayon-line" id="crayon-5768534d43557636513993-1">
                 <span class="crayon-st">
                  IF
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-e">
                  OBJECT_ID
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-s">
                  'News'
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-s">
                  'U'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-st">
                  IS
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-st">
                  NOT
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-t">
                  NULL
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-e">
                  DROP
                 </span>
                 <span class="crayon-e">
                  TABLE
                 </span>
                 <span class="crayon-e">
                  News
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d43557636513993-2">
                 <span class="crayon-e">
                  GO
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d43557636513993-3">
                 <span class="crayon-e">
                  CREATE
                 </span>
                 <span class="crayon-e">
                  TABLE
                 </span>
                 <span class="crayon-e">
                  News
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d43557636513993-4">
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-e">
                  ID
                 </span>
                 <span class="crayon-t">
                  INT
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-st">
                  NOT
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-t">
                  NULL
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-e">
                  PRIMARY
                 </span>
                 <span class="crayon-e">
                  KEY
                 </span>
                 <span class="crayon-e">
                  IDENTITY
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-cn">
                  1
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-cn">
                  1
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d43557636513993-5">
                 <span class="crayon-e">
                  NAME
                 </span>
                 <span class="crayon-e">
                  NVARCHAR
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-cn">
                  100
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-st">
                  NOT
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-t">
                  NULL
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d43557636513993-6">
                 <span class="crayon-e">
                  Createdate
                 </span>
                 <span class="crayon-e">
                  DATETIME
                 </span>
                 <span class="crayon-st">
                  NOT
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-t">
                  NULL
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d43557636513993-7">
                 <span class="crayon-sy">
                  )
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d43557636513993-8">
                 <span class="crayon-e">
                  GO
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d43557636513993-9">
                 <span class="crayon-e">
                  CREATE
                 </span>
                 <span class="crayon-e">
                  NONCLUSTERED
                 </span>
                 <span class="crayon-i">
                  INDEX
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-sy">
                  [
                 </span>
                 <span class="crayon-v">
                  IX1_News
                 </span>
                 <span class="crayon-sy">
                  ]
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-i">
                  ON
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-sy">
                  [
                 </span>
                 <span class="crayon-v">
                  dbo
                 </span>
                 <span class="crayon-sy">
                  ]
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-sy">
                  [
                 </span>
                 <span class="crayon-v">
                  News
                 </span>
                 <span class="crayon-sy">
                  ]
                 </span>
                 <span class="crayon-h">
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d43557636513993-10">
                 <span class="crayon-sy">
                  (
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d43557636513993-11">
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-sy">
                  [
                 </span>
                 <span class="crayon-v">
                  Createdate
                 </span>
                 <span class="crayon-sy">
                  ]
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-i">
                  ASC
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d43557636513993-12">
                 <span class="crayon-sy">
                  )
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d43557636513993-13">
                 <span class="crayon-e">
                  INCLUDE
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-sy">
                  [
                 </span>
                 <span class="crayon-v">
                  NAME
                 </span>
                 <span class="crayon-sy">
                  ]
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-e">
                  WITH
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-v">
                  STATISTICS_NORECOMPUTE
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  OFF
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  SORT_IN_TEMPDB
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  OFF
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  IGNORE_DUP_KEY
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  OFF
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  DROP_EXISTING
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  OFF
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  ONLINE
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  OFF
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  ALLOW_ROW_LOCKS
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  ON
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  ALLOW_PAGE_LOCKS
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  ON
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-i">
                  ON
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-sy">
                  [
                 </span>
                 <span class="crayon-v">
                  PRIMARY
                 </span>
                 <span class="crayon-sy">
                  ]
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d43557636513993-14">
                 <span class="crayon-e">
                  GO
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d43557636513993-15">
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d43557636513993-16">
                 <span class="crayon-e">
                  GO
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d43557636513993-17">
                 <span class="crayon-e">
                  INSERT
                 </span>
                 <span class="crayon-e">
                  INTO
                 </span>
                 <span class="crayon-e">
                  News
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-v">
                  NAME
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-v">
                  Createdate
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-h">
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d43557636513993-18">
                 <span class="crayon-e">
                  VALUES
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-s">
                  '新闻'
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-s">
                  '2014-08-20 00:00:00'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-s">
                  '新闻'
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-s">
                  '2014-08-20 00:00:00'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-s">
                  '新闻'
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-s">
                  '2014-08-20 00:00:00'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-s">
                  '新闻'
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-s">
                  '2014-08-20 00:00:00'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                </div>
               </div>
              </td>
             </tr>
            </tbody>
           </table>
          </div>
         </div>
         <!-- [Format Time: 0.0418 seconds] -->
         <p>
         </p>
         <div class="cnblogs_code_toolbar">
          <span class="cnblogs_code_copy">
           <p title="复制代码">
            <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
           </p>
          </span>
         </div>
        </div>
        <p>
        </p>
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d4355f145137408" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d4355f145137408-1">
                1
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d4355f145137408-1">
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <p>
         —使用计算列查询（走的是索引扫描）
        </p>
        <div class="cnblogs_code">
         <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
         <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43566407235078" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
          <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <span class="crayon-title">
           </span>
           <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
            <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-wrap-button" title="切换自动换行">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-expand-button" title="点击展开代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-copy-button" title="复制代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
           </div>
          </div>
          <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
          </div>
          <div class="crayon-plain-wrap">
           <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
            SELECT ID,NAME,Createdate FROM News
WHERE YEAR(Createdate)=2014
           </textarea>
          </div>
          <div class="crayon-main" style="">
           <table class="crayon-table">
            <tbody>
             <tr class="crayon-row">
              <td class="crayon-nums " data-settings="show">
               <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
                <div class="crayon-num" data-line="crayon-5768534d43566407235078-1">
                 1
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43566407235078-2">
                 2
                </div>
               </div>
              </td>
              <td class="crayon-code">
               <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
                <div class="crayon-line" id="crayon-5768534d43566407235078-1">
                 <span class="crayon-e">
                  SELECT
                 </span>
                 <span class="crayon-v">
                  ID
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-v">
                  NAME
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-e">
                  Createdate
                 </span>
                 <span class="crayon-e">
                  FROM
                 </span>
                 <span class="crayon-e">
                  News
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d43566407235078-2">
                 <span class="crayon-e">
                  WHERE
                 </span>
                 <span class="crayon-e">
                  YEAR
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-v">
                  Createdate
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-cn">
                  2014
                 </span>
                </div>
               </div>
              </td>
             </tr>
            </tbody>
           </table>
          </div>
         </div>
         <!-- [Format Time: 0.0010 seconds] -->
        </div>
        <p>
         <img src="http://dataunion.org/wp-content/uploads/2015/05/290955349561459.png"/>
        </p>
        <p>
         —不使用计算列（走的是索引查找）
        </p>
        <div class="cnblogs_code">
         <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
         <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d4356c709221590" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
          <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <span class="crayon-title">
           </span>
           <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
            <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-wrap-button" title="切换自动换行">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-expand-button" title="点击展开代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-copy-button" title="复制代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
           </div>
          </div>
          <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
          </div>
          <div class="crayon-plain-wrap">
           <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
            SELECT ID,NAME,Createdate FROM News
WHERE CreateDate&amp;gt;='2014-01-01 00:00:00' and CreateDate&amp;lt;'2015-01-01 00:00:00'
           </textarea>
          </div>
          <div class="crayon-main" style="">
           <table class="crayon-table">
            <tbody>
             <tr class="crayon-row">
              <td class="crayon-nums " data-settings="show">
               <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
                <div class="crayon-num" data-line="crayon-5768534d4356c709221590-1">
                 1
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4356c709221590-2">
                 2
                </div>
               </div>
              </td>
              <td class="crayon-code">
               <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
                <div class="crayon-line" id="crayon-5768534d4356c709221590-1">
                 <span class="crayon-e">
                  SELECT
                 </span>
                 <span class="crayon-v">
                  ID
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-v">
                  NAME
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-e">
                  Createdate
                 </span>
                 <span class="crayon-e">
                  FROM
                 </span>
                 <span class="crayon-e">
                  News
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d4356c709221590-2">
                 <span class="crayon-e">
                  WHERE
                 </span>
                 <span class="crayon-v">
                  CreateDate
                 </span>
                 <span class="crayon-o">
                  &amp;
                 </span>
                 <span class="crayon-v">
                  gt
                 </span>
                 <span class="crayon-sy">
                  ;
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-s">
                  '2014-01-01 00:00:00'
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-st">
                  and
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  CreateDate
                 </span>
                 <span class="crayon-o">
                  &amp;
                 </span>
                 <span class="crayon-v">
                  lt
                 </span>
                 <span class="crayon-sy">
                  ;
                 </span>
                 <span class="crayon-s">
                  '2015-01-01 00:00:00'
                 </span>
                </div>
               </div>
              </td>
             </tr>
            </tbody>
           </table>
          </div>
         </div>
         <!-- [Format Time: 0.0013 seconds] -->
        </div>
        <p>
         <img src="http://dataunion.org/wp-content/uploads/2015/05/290956418788438.png"/>
        </p>
        <p>
         对比两个查询显然绝大部分情况下走索引查找的查询性能要高于走索引扫描，特别是查询的数据库不是非常大的情况下，索引查找的消耗时间要远远少于索引扫描的时间，如果想详细了解索引的体系结构可以查看了我前面写的几篇关于聚集、非聚集、堆的索引体系机构的文章。
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/291000245504032.png"/>
       </p>
       <p>
        请参看：
        <a href="http://www.cnblogs.com/chenmh/p/3780221.html" target="_blank" title="聚集索引">
         <u>
          http://www.cnblogs.com/chenmh/p/3780221.html
         </u>
        </a>
       </p>
       <p>
        <u>
         <img src="http://dataunion.org/wp-content/uploads/2015/05/291002318164212.png"/>
        </u>
       </p>
       <p>
        请参看：
        <a href="http://www.cnblogs.com/chenmh/p/3782397.html" target="_blank" title="非聚集索引">
         <u>
          http://www.cnblogs.com/chenmh/p/3782397.html
         </u>
        </a>
       </p>
       <p>
       </p>
       <h3>
        <strong>
         建表时字段不允许为null
        </strong>
       </h3>
       <p>
        <strong>
         <br/>
        </strong>
        发现很多人在建表的时候不会注意这一点，在接下来的工作中当你需要查询数据的时候你往往需要在WHERE条件中多加一个判断条件IS NOT NULL,这样的一个条件不仅仅增加了额外的开销，而且对查询的性能产生很大的影响，有可能就因为多了这个查询条件导致你的查询变的非常的慢；还有一个比较重要的问题就是允许为空的数据可能会导致你的查询结果出现不准确的问题，接下来我们就举个例子讨论一下。
       </p>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43573448642609" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           T-SQL是三值逻辑（true,flase,unknown）
IF OBJECT_ID('DBO.Customer','U') IS NOT NULL DROP TABLE DBO.Customer
GO
CREATE TABLE DBO.Customer
(Customerid int not null );
GO
IF OBJECT_ID('DBO.OrderS','U') IS NOT NULL DROP TABLE DBO.OrderS
GO
CREATE TABLE DBO.OrderS
(Orderid int not null,
custid int);
GO
INSERT INTO Customer VALUES(1),(2),(3);
INSERT INTO OrderS VALUES(1,1),(2,2),(3,NULL);

----查询没有订单的顾客
SELECT Customerid FROM DBO.Customer WHERE Customerid NOT IN(SELECT custid FROM OrderS);

---分析为什么查询结果没有数据
/*
因为true,flase,unknown都是真值
因为not in 是需要结果中返回flase值,not true=flase,not flase=flase,not unknown=unknown
因为null值是unknown所以not unknownn无法判断结果是什么值所以不能返回数据
*/

--可以将查询语句修改为
SELECT Customerid FROM DBO.Customer WHERE Customerid NOT IN(SELECT custid FROM OrderS WHERE custid is not null);
--或者使用EXISTS，因为EXISTS是二值逻辑只有（true,flase）所以不存在未知。
SELECT Customerid FROM DBO.Customer A WHERE  NOT EXISTS(SELECT custid FROM OrderS WHERE OrderS.custid=A.Customerid );

---in查询可以返回值，因为in是true，子查询true,flase,unknown都是真值所以可以返回子查询的true
SELECT Customerid FROM DBO.Customer WHERE Customerid  IN(SELECT custid FROM OrderS);


----如果整形字段可以赋0，字符型可以赋值空（这里只是给建议）这里的空和NULL是不一样的意思
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-13">
                13
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-14">
                14
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-15">
                15
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-16">
                16
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-17">
                17
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-18">
                18
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-19">
                19
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-20">
                20
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-21">
                21
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-22">
                22
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-23">
                23
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-24">
                24
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-25">
                25
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-26">
                26
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-27">
                27
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-28">
                28
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-29">
                29
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-30">
                30
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-31">
                31
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-32">
                32
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-33">
                33
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43573448642609-34">
                34
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43573448642609-35">
                35
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43573448642609-1">
                <span class="crayon-v">
                 T
                </span>
                <span class="crayon-o">
                 -
                </span>
                <span class="crayon-i">
                 SQL
                </span>
                是三值逻辑（
                <span class="crayon-t">
                 true
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 flase
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-i">
                 unknown
                </span>
                ）
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-2">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 OBJECT_ID
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 'DBO.Customer'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 'U'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 IS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 DROP
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customer
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-3">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-4">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customer
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-5">
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 not
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 null
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-6">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-7">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 OBJECT_ID
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 'DBO.OrderS'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 'U'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 IS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 DROP
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-8">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-9">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-10">
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 Orderid
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 not
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 null
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-11">
                <span class="crayon-e">
                 custid
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-12">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-13">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Customer
                </span>
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 3
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-14">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 3
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-15">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-16">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 --
                </span>
                查询没有订单的顾客
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-17">
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customer
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 IN
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-e">
                 custid
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-v">
                 OrderS
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-18">
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-19">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                分析为什么查询结果没有数据
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-20">
                <span class="crayon-c">
                 /*
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-21">
                <span class="crayon-c">
                 因为true,flase,unknown都是真值
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-22">
                <span class="crayon-c">
                 因为not in 是需要结果中返回flase值,not true=flase,not flase=flase,not unknown=unknown
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-23">
                <span class="crayon-c">
                 因为null值是unknown所以not unknownn无法判断结果是什么值所以不能返回数据
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-24">
                <span class="crayon-c">
                 */
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-25">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-26">
                <span class="crayon-o">
                 --
                </span>
                可以将查询语句修改为
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-27">
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customer
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 IN
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-e">
                 custid
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-e">
                 custid
                </span>
                <span class="crayon-st">
                 is
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 not
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 null
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-28">
                <span class="crayon-o">
                 --
                </span>
                或者使用
                <span class="crayon-i">
                 EXISTS
                </span>
                ，因为
                <span class="crayon-i">
                 EXISTS
                </span>
                是二值逻辑只有（
                <span class="crayon-t">
                 true
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-i">
                 flase
                </span>
                ）所以不存在未知。
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-29">
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-i">
                 Customer
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 A
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 EXISTS
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-e">
                 custid
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-v">
                 OrderS
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 custid
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-v">
                 A
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-i">
                 Customerid
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-30">
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-31">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                <span class="crayon-st">
                 in
                </span>
                查询可以返回值，因为
                <span class="crayon-st">
                 in
                </span>
                是
                <span class="crayon-t">
                 true
                </span>
                ，子查询
                <span class="crayon-t">
                 true
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 flase
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-i">
                 unknown
                </span>
                都是真值所以可以返回子查询的
                <span class="crayon-t">
                 true
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-32">
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customer
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-st">
                 IN
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-e">
                 custid
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-v">
                 OrderS
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-33">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43573448642609-34">
               </div>
               <div class="crayon-line" id="crayon-5768534d43573448642609-35">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 --
                </span>
                如果整形字段可以赋
                <span class="crayon-cn">
                 0
                </span>
                ，字符型可以赋值空（这里只是给建议）这里的空和
                <span class="crayon-t">
                 NULL
                </span>
                是不一样的意思
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0485 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code">
         <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
         <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d4357b316430725" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
          <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <span class="crayon-title">
           </span>
           <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
            <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-wrap-button" title="切换自动换行">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-expand-button" title="点击展开代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-copy-button" title="复制代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
           </div>
          </div>
          <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
          </div>
          <div class="crayon-plain-wrap">
           <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
            --增加整形字段可以这样写
ALTER TABLE TABLE_NAME ADD  COLUMN_NAME  INT NOT NULL DEFAULT(0)

--增加字符型字段可以这样写
ALTER TABLE TABLE_NAME ADD  COLUMN_NAME  NVARCHAR(50) NOT NULL DEFAULT('')
           </textarea>
          </div>
          <div class="crayon-main" style="">
           <table class="crayon-table">
            <tbody>
             <tr class="crayon-row">
              <td class="crayon-nums " data-settings="show">
               <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
                <div class="crayon-num" data-line="crayon-5768534d4357b316430725-1">
                 1
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4357b316430725-2">
                 2
                </div>
                <div class="crayon-num" data-line="crayon-5768534d4357b316430725-3">
                 3
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4357b316430725-4">
                 4
                </div>
                <div class="crayon-num" data-line="crayon-5768534d4357b316430725-5">
                 5
                </div>
               </div>
              </td>
              <td class="crayon-code">
               <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
                <div class="crayon-line" id="crayon-5768534d4357b316430725-1">
                 <span class="crayon-o">
                  --
                 </span>
                 增加整形字段可以这样写
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d4357b316430725-2">
                 <span class="crayon-e">
                  ALTER
                 </span>
                 <span class="crayon-e">
                  TABLE
                 </span>
                 <span class="crayon-e">
                  TABLE_NAME
                 </span>
                 <span class="crayon-e">
                  ADD
                 </span>
                 <span class="crayon-e">
                  COLUMN_NAME
                 </span>
                 <span class="crayon-t">
                  INT
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-st">
                  NOT
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-t">
                  NULL
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-st">
                  DEFAULT
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-cn">
                  0
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d4357b316430725-3">
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d4357b316430725-4">
                 <span class="crayon-o">
                  --
                 </span>
                 增加字符型字段可以这样写
                </div>
                <div class="crayon-line" id="crayon-5768534d4357b316430725-5">
                 <span class="crayon-e">
                  ALTER
                 </span>
                 <span class="crayon-e">
                  TABLE
                 </span>
                 <span class="crayon-e">
                  TABLE_NAME
                 </span>
                 <span class="crayon-e">
                  ADD
                 </span>
                 <span class="crayon-e">
                  COLUMN_NAME
                 </span>
                 <span class="crayon-e">
                  NVARCHAR
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-cn">
                  50
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-st">
                  NOT
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-t">
                  NULL
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-st">
                  DEFAULT
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-s">
                  ''
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                </div>
               </div>
              </td>
             </tr>
            </tbody>
           </table>
          </div>
         </div>
         <!-- [Format Time: 0.0119 seconds] -->
        </div>
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
       </p>
       <h3>
        <strong>
         分组统计时避免使用count(*)
        </strong>
       </h3>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43581036237803" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           IF OBJECT_ID('DBO.Customer','U') IS NOT NULL DROP TABLE DBO.Customer
GO
CREATE TABLE DBO.Customer
(Customerid int not null );
GO
IF OBJECT_ID('DBO.OrderS','U') IS NOT NULL DROP TABLE DBO.OrderS
GO
CREATE TABLE DBO.OrderS
(Orderid int not null,
custid int);
GO
INSERT INTO Customer VALUES(1),(2),(3);
INSERT INTO OrderS VALUES(1,1),(2,2),(3,NULL);
例如：需要统计每一个顾客的订单数量
---如果使用count(*)
SELECT Customerid,COUNT(*) FROM Customer TA LEFT JOIN OrderS TB ON TA.Customerid=TB.custid 
GROUP BY Customerid ;
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43581036237803-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43581036237803-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43581036237803-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43581036237803-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43581036237803-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43581036237803-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43581036237803-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43581036237803-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43581036237803-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43581036237803-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43581036237803-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43581036237803-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43581036237803-13">
                13
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43581036237803-14">
                14
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43581036237803-15">
                15
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43581036237803-16">
                16
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43581036237803-17">
                17
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43581036237803-1">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 OBJECT_ID
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 'DBO.Customer'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 'U'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 IS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 DROP
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customer
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43581036237803-2">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43581036237803-3">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customer
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43581036237803-4">
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 not
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 null
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43581036237803-5">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43581036237803-6">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 OBJECT_ID
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 'DBO.OrderS'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 'U'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 IS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 DROP
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43581036237803-7">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43581036237803-8">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43581036237803-9">
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 Orderid
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 not
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 null
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43581036237803-10">
                <span class="crayon-e">
                 custid
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43581036237803-11">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43581036237803-12">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Customer
                </span>
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 3
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43581036237803-13">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 3
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43581036237803-14">
                例如：需要统计每一个顾客的订单数量
               </div>
               <div class="crayon-line" id="crayon-5768534d43581036237803-15">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                如果使用
                <span class="crayon-e">
                 count
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-o">
                 *
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43581036237803-16">
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-v">
                 Customerid
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-e">
                 COUNT
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-o">
                 *
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 Customer
                </span>
                <span class="crayon-e">
                 TA
                </span>
                <span class="crayon-e">
                 LEFT
                </span>
                <span class="crayon-e">
                 JOIN
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
                <span class="crayon-e">
                 TB
                </span>
                <span class="crayon-e">
                 ON
                </span>
                <span class="crayon-v">
                 TA
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 Customerid
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-v">
                 TB
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 custid
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43581036237803-17">
                <span class="crayon-e">
                 GROUP
                </span>
                <span class="crayon-e">
                 BY
                </span>
                <span class="crayon-i">
                 Customerid
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0316 seconds] -->
        <p>
         <img src="http://dataunion.org/wp-content/uploads/2015/05/291052168168146.png"/>
        </p>
        <p align="left">
         实际情况customerid=3是没有订单的，数量应该是0，但是结果是1,count()里面的字段是左连接右边的表字段，如果你用的是主表字段结果页是错误的。
        </p>
        <div class="cnblogs_code">
         <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
         <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43588062362095" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
          <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <span class="crayon-title">
           </span>
           <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
            <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-wrap-button" title="切换自动换行">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-expand-button" title="点击展开代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-copy-button" title="复制代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
           </div>
          </div>
          <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
          </div>
          <div class="crayon-plain-wrap">
           <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
            ----正确的方法是使用count(custid)
SELECT Customerid,COUNT(custid) FROM Customer TA LEFT JOIN OrderS TB ON TA.Customerid=TB.custid 
GROUP BY Customerid;
           </textarea>
          </div>
          <div class="crayon-main" style="">
           <table class="crayon-table">
            <tbody>
             <tr class="crayon-row">
              <td class="crayon-nums " data-settings="show">
               <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
                <div class="crayon-num" data-line="crayon-5768534d43588062362095-1">
                 1
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43588062362095-2">
                 2
                </div>
                <div class="crayon-num" data-line="crayon-5768534d43588062362095-3">
                 3
                </div>
               </div>
              </td>
              <td class="crayon-code">
               <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
                <div class="crayon-line" id="crayon-5768534d43588062362095-1">
                 <span class="crayon-o">
                  --
                 </span>
                 <span class="crayon-o">
                  --
                 </span>
                 正确的方法是使用
                 <span class="crayon-e">
                  count
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-v">
                  custid
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d43588062362095-2">
                 <span class="crayon-e">
                  SELECT
                 </span>
                 <span class="crayon-v">
                  Customerid
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-e">
                  COUNT
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-v">
                  custid
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-e">
                  FROM
                 </span>
                 <span class="crayon-e">
                  Customer
                 </span>
                 <span class="crayon-e">
                  TA
                 </span>
                 <span class="crayon-e">
                  LEFT
                 </span>
                 <span class="crayon-e">
                  JOIN
                 </span>
                 <span class="crayon-e">
                  OrderS
                 </span>
                 <span class="crayon-e">
                  TB
                 </span>
                 <span class="crayon-e">
                  ON
                 </span>
                 <span class="crayon-v">
                  TA
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  Customerid
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-v">
                  TB
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-e">
                  custid
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d43588062362095-3">
                 <span class="crayon-e">
                  GROUP
                 </span>
                 <span class="crayon-e">
                  BY
                 </span>
                 <span class="crayon-v">
                  Customerid
                 </span>
                 <span class="crayon-sy">
                  ;
                 </span>
                </div>
               </div>
              </td>
             </tr>
            </tbody>
           </table>
          </div>
         </div>
         <!-- [Format Time: 0.0018 seconds] -->
        </div>
        <p>
         <img src="http://dataunion.org/wp-content/uploads/2015/05/291053196282937.png"/>
        </p>
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
       </p>
       <h3>
        <strong>
         子查询的表加上表别名
        </strong>
       </h3>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d4358e275470335" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           IF OBJECT_ID('DBO.Customer','U') IS NOT NULL DROP TABLE DBO.Customer
GO
CREATE TABLE DBO.Customer
(Customerid int not null );
GO
IF OBJECT_ID('DBO.OrderS','U') IS NOT NULL DROP TABLE DBO.OrderS
GO
CREATE TABLE DBO.OrderS
(Orderid int not null,
custid int);
GO
INSERT INTO Customer VALUES(1),(2),(3);
INSERT INTO OrderS VALUES(1,1),(2,2),(3,NULL);
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d4358e275470335-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4358e275470335-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4358e275470335-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4358e275470335-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4358e275470335-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4358e275470335-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4358e275470335-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4358e275470335-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4358e275470335-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4358e275470335-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4358e275470335-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4358e275470335-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4358e275470335-13">
                13
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d4358e275470335-1">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 OBJECT_ID
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 'DBO.Customer'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 'U'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 IS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 DROP
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customer
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4358e275470335-2">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4358e275470335-3">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customer
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4358e275470335-4">
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 not
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 null
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4358e275470335-5">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4358e275470335-6">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 OBJECT_ID
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 'DBO.OrderS'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 'U'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 IS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 DROP
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4358e275470335-7">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4358e275470335-8">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4358e275470335-9">
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 Orderid
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 not
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 null
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4358e275470335-10">
                <span class="crayon-e">
                 custid
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4358e275470335-11">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4358e275470335-12">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Customer
                </span>
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 3
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4358e275470335-13">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 3
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0093 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
        大家发现下面语句有没有什么问题，查询结果是怎样呢？
       </p>
       <div class="cnblogs_code">
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43594766862817" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           SELECT Customerid FROM Customer WHERE Customerid IN(SELECT Customerid FROM OrderS WHERE Orderid=2 );
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43594766862817-1">
                1
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43594766862817-1">
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 Customer
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-st">
                 IN
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-v">
                 Orderid
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0011 seconds] -->
       </div>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/291100025197821.png"/>
       </p>
       <p>
        <strong>
         <br/>
        </strong>
        正确查询结果下查询出的结果是没有customerid为3的值
       </p>
       <p>
        为什么结果会这样呢？
       </p>
       <p>
        大家仔细看应该会发现子查询的orders表中没有Customerid字段，所以SQL取的是Customer表的Customerid值作为相关子查询的匹配字段。
       </p>
       <p>
        所以我们应该给子查询加上表别名，如果加上表别名，如果字段错误的话会有错误标示
       </p>
       <p>
        正确的写法：
       </p>
       <div class="cnblogs_code">
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d4359a824012627" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           SELECT Customerid  FROM Customer WHERE Customerid IN(SELECT tb.custid   FROM OrderS tb WHERE Orderid=2 );
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d4359a824012627-1">
                1
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d4359a824012627-1">
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 Customer
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-st">
                 IN
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-v">
                 tb
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 custid
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
                <span class="crayon-e">
                 tb
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-v">
                 Orderid
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0013 seconds] -->
       </div>
       <h3>
        <strong>
         建立自增列时单独再给自增列添加唯一约束
        </strong>
       </h3>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <div class="cnblogs_code">
         <div class="cnblogs_code_toolbar">
          <span class="cnblogs_code_copy">
           <p title="复制代码">
            <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
           </p>
          </span>
         </div>
         <p>
         </p>
         <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
         <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d435a1842392582" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
          <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <span class="crayon-title">
           </span>
           <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
            <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-wrap-button" title="切换自动换行">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-expand-button" title="点击展开代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-copy-button" title="复制代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
           </div>
          </div>
          <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
          </div>
          <div class="crayon-plain-wrap">
           <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
            USE tempdb 
CREATE TABLE TEST
(ID INT NOT NULL IDENTITY(1,1),
orderdate date NOT NULL DEFAULT(CURRENT_TIMESTAMP),
NAME NVARCHAR(30) NOT NULL,
CONSTRAINT CK_TEST_NAME CHECK(NAME LIKE '[A-Za-z]%' ) 
);

GO
INSERT INTO tempdb.DBO.TEST(NAME)
VALUES('A中'),('a名'),('Aa'),('ab'),('AA'),('az');

----4.插入报错后，自增值依旧增加
INSERT INTO tempdb.DBO.TEST(NAME)
VALUES('中');
GO
SELECT IDENT_CURRENT('tempdb.DBO.TEST');
SELECT * FROM tempdb.DBO.TEST;

---插入正常的数据
INSERT INTO tempdb.DBO.TEST(NAME)
VALUES('cc');

SELECT IDENT_CURRENT('tempdb.DBO.TEST')
SELECT * FROM tempdb.DBO.TEST;


----5.显示插入自增值
SET IDENTITY_INSERT tempdb.DBO.TEST ON

INSERT INTO tempdb.DBO.TEST(ID,NAME)
VALUES(8,'A中');

SET IDENTITY_INSERT tempdb.DBO.TEST OFF

----会发现ID并不是根据自增值排列的，而且根据插入的顺序排列的
SELECT IDENT_CURRENT('tempdb.DBO.TEST');
SELECT * FROM tempdb.DBO.TEST;

----6.插入重复的自增值
SET IDENTITY_INSERT tempdb.DBO.TEST ON

INSERT INTO tempdb.DBO.TEST(ID,NAME)
VALUES(8,'A中');

SET IDENTITY_INSERT tempdb.DBO.TEST OFF

SELECT IDENT_CURRENT('tempdb.DBO.TEST')
SELECT * FROM tempdb.DBO.TEST;
---所以如果要保证ID是唯一的，单单只设置自增值不行，需要给字段设置主键或者唯一约束
DROP TABLE tempdb.DBO.TEST;
           </textarea>
          </div>
          <div class="crayon-main" style="">
           <table class="crayon-table">
            <tbody>
             <tr class="crayon-row">
              <td class="crayon-nums " data-settings="show">
               <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-1">
                 1
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-2">
                 2
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-3">
                 3
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-4">
                 4
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-5">
                 5
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-6">
                 6
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-7">
                 7
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-8">
                 8
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-9">
                 9
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-10">
                 10
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-11">
                 11
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-12">
                 12
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-13">
                 13
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-14">
                 14
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-15">
                 15
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-16">
                 16
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-17">
                 17
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-18">
                 18
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-19">
                 19
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-20">
                 20
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-21">
                 21
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-22">
                 22
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-23">
                 23
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-24">
                 24
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-25">
                 25
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-26">
                 26
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-27">
                 27
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-28">
                 28
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-29">
                 29
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-30">
                 30
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-31">
                 31
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-32">
                 32
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-33">
                 33
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-34">
                 34
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-35">
                 35
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-36">
                 36
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-37">
                 37
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-38">
                 38
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-39">
                 39
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-40">
                 40
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-41">
                 41
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-42">
                 42
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-43">
                 43
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-44">
                 44
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-45">
                 45
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-46">
                 46
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-47">
                 47
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-48">
                 48
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-49">
                 49
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a1842392582-50">
                 50
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435a1842392582-51">
                 51
                </div>
               </div>
              </td>
              <td class="crayon-code">
               <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
                <div class="crayon-line" id="crayon-5768534d435a1842392582-1">
                 <span class="crayon-st">
                  USE
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-e">
                  tempdb
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-2">
                 <span class="crayon-e">
                  CREATE
                 </span>
                 <span class="crayon-e">
                  TABLE
                 </span>
                 <span class="crayon-e">
                  TEST
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-3">
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-e">
                  ID
                 </span>
                 <span class="crayon-t">
                  INT
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-st">
                  NOT
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-t">
                  NULL
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-e">
                  IDENTITY
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-cn">
                  1
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-cn">
                  1
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-4">
                 <span class="crayon-e">
                  orderdate
                 </span>
                 <span class="crayon-e">
                  date
                 </span>
                 <span class="crayon-st">
                  NOT
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-t">
                  NULL
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-st">
                  DEFAULT
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-v">
                  CURRENT_TIMESTAMP
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-5">
                 <span class="crayon-e">
                  NAME
                 </span>
                 <span class="crayon-e">
                  NVARCHAR
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-cn">
                  30
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-st">
                  NOT
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-t">
                  NULL
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-6">
                 <span class="crayon-e">
                  CONSTRAINT
                 </span>
                 <span class="crayon-e">
                  CK_TEST_NAME
                 </span>
                 <span class="crayon-e">
                  CHECK
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-e">
                  NAME
                 </span>
                 <span class="crayon-i">
                  LIKE
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-s">
                  '[A-Za-z]%'
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-h">
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-7">
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ;
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-8">
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-9">
                 <span class="crayon-e">
                  GO
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-10">
                 <span class="crayon-e">
                  INSERT
                 </span>
                 <span class="crayon-e">
                  INTO
                 </span>
                 <span class="crayon-v">
                  tempdb
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  DBO
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-e">
                  TEST
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-v">
                  NAME
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-11">
                 <span class="crayon-e">
                  VALUES
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-s">
                  'A中'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-s">
                  'a名'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-s">
                  'Aa'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-s">
                  'ab'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-s">
                  'AA'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-s">
                  'az'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ;
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-12">
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-13">
                 <span class="crayon-o">
                  --
                 </span>
                 <span class="crayon-o">
                  --
                 </span>
                 <span class="crayon-cn">
                  4.
                 </span>
                 插入报错后，自增值依旧增加
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-14">
                 <span class="crayon-e">
                  INSERT
                 </span>
                 <span class="crayon-e">
                  INTO
                 </span>
                 <span class="crayon-v">
                  tempdb
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  DBO
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-e">
                  TEST
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-v">
                  NAME
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-15">
                 <span class="crayon-e">
                  VALUES
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-s">
                  '中'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ;
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-16">
                 <span class="crayon-e">
                  GO
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-17">
                 <span class="crayon-e">
                  SELECT
                 </span>
                 <span class="crayon-e">
                  IDENT_CURRENT
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-s">
                  'tempdb.DBO.TEST'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ;
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-18">
                 <span class="crayon-e ">
                  SELECT *
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-e">
                  FROM
                 </span>
                 <span class="crayon-v">
                  tempdb
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  DBO
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  TEST
                 </span>
                 <span class="crayon-sy">
                  ;
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-19">
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-20">
                 <span class="crayon-o">
                  --
                 </span>
                 <span class="crayon-o">
                  -
                 </span>
                 插入正常的数据
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-21">
                 <span class="crayon-e">
                  INSERT
                 </span>
                 <span class="crayon-e">
                  INTO
                 </span>
                 <span class="crayon-v">
                  tempdb
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  DBO
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-e">
                  TEST
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-v">
                  NAME
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-22">
                 <span class="crayon-e">
                  VALUES
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-s">
                  'cc'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ;
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-23">
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-24">
                 <span class="crayon-e">
                  SELECT
                 </span>
                 <span class="crayon-e">
                  IDENT_CURRENT
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-s">
                  'tempdb.DBO.TEST'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-25">
                 <span class="crayon-e ">
                  SELECT *
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-e">
                  FROM
                 </span>
                 <span class="crayon-v">
                  tempdb
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  DBO
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  TEST
                 </span>
                 <span class="crayon-sy">
                  ;
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-26">
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-27">
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-28">
                 <span class="crayon-o">
                  --
                 </span>
                 <span class="crayon-o">
                  --
                 </span>
                 <span class="crayon-cn">
                  5.
                 </span>
                 显示插入自增值
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-29">
                 <span class="crayon-e">
                  SET
                 </span>
                 <span class="crayon-e">
                  IDENTITY_INSERT
                 </span>
                 <span class="crayon-v">
                  tempdb
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  DBO
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-e">
                  TEST
                 </span>
                 <span class="crayon-e">
                  ON
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-30">
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-31">
                 <span class="crayon-e">
                  INSERT
                 </span>
                 <span class="crayon-e">
                  INTO
                 </span>
                 <span class="crayon-v">
                  tempdb
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  DBO
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-e">
                  TEST
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-v">
                  ID
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-v">
                  NAME
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-32">
                 <span class="crayon-e">
                  VALUES
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-cn">
                  8
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-s">
                  'A中'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ;
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-33">
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-34">
                 <span class="crayon-e">
                  SET
                 </span>
                 <span class="crayon-e">
                  IDENTITY_INSERT
                 </span>
                 <span class="crayon-v">
                  tempdb
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  DBO
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-e">
                  TEST
                 </span>
                 <span class="crayon-v">
                  OFF
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-35">
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-36">
                 <span class="crayon-o">
                  --
                 </span>
                 <span class="crayon-o">
                  --
                 </span>
                 会发现
                 <span class="crayon-i">
                  ID
                 </span>
                 并不是根据自增值排列的，而且根据插入的顺序排列的
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-37">
                 <span class="crayon-e">
                  SELECT
                 </span>
                 <span class="crayon-e">
                  IDENT_CURRENT
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-s">
                  'tempdb.DBO.TEST'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ;
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-38">
                 <span class="crayon-e ">
                  SELECT *
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-e">
                  FROM
                 </span>
                 <span class="crayon-v">
                  tempdb
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  DBO
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  TEST
                 </span>
                 <span class="crayon-sy">
                  ;
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-39">
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-40">
                 <span class="crayon-o">
                  --
                 </span>
                 <span class="crayon-o">
                  --
                 </span>
                 <span class="crayon-cn">
                  6.
                 </span>
                 插入重复的自增值
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-41">
                 <span class="crayon-e">
                  SET
                 </span>
                 <span class="crayon-e">
                  IDENTITY_INSERT
                 </span>
                 <span class="crayon-v">
                  tempdb
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  DBO
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-e">
                  TEST
                 </span>
                 <span class="crayon-e">
                  ON
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-42">
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-43">
                 <span class="crayon-e">
                  INSERT
                 </span>
                 <span class="crayon-e">
                  INTO
                 </span>
                 <span class="crayon-v">
                  tempdb
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  DBO
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-e">
                  TEST
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-v">
                  ID
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-v">
                  NAME
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-44">
                 <span class="crayon-e">
                  VALUES
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-cn">
                  8
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-s">
                  'A中'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                 <span class="crayon-sy">
                  ;
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-45">
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-46">
                 <span class="crayon-e">
                  SET
                 </span>
                 <span class="crayon-e">
                  IDENTITY_INSERT
                 </span>
                 <span class="crayon-v">
                  tempdb
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  DBO
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-e">
                  TEST
                 </span>
                 <span class="crayon-e">
                  OFF
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-47">
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-48">
                 <span class="crayon-e">
                  SELECT
                 </span>
                 <span class="crayon-e">
                  IDENT_CURRENT
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-s">
                  'tempdb.DBO.TEST'
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-49">
                 <span class="crayon-e ">
                  SELECT *
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-e">
                  FROM
                 </span>
                 <span class="crayon-v">
                  tempdb
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  DBO
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  TEST
                 </span>
                 <span class="crayon-sy">
                  ;
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a1842392582-50">
                 <span class="crayon-o">
                  --
                 </span>
                 <span class="crayon-o">
                  -
                 </span>
                 所以如果要保证
                 <span class="crayon-i">
                  ID
                 </span>
                 是唯一的，单单只设置自增值不行，需要给字段设置主键或者唯一约束
                </div>
                <div class="crayon-line" id="crayon-5768534d435a1842392582-51">
                 <span class="crayon-e">
                  DROP
                 </span>
                 <span class="crayon-e">
                  TABLE
                 </span>
                 <span class="crayon-v">
                  tempdb
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  DBO
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  TEST
                 </span>
                 <span class="crayon-sy">
                  ;
                 </span>
                </div>
               </div>
              </td>
             </tr>
            </tbody>
           </table>
          </div>
         </div>
         <!-- [Format Time: 0.0275 seconds] -->
         <p>
         </p>
         <div class="cnblogs_code_toolbar">
          <span class="cnblogs_code_copy">
           <p title="复制代码">
            <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
           </p>
          </span>
         </div>
        </div>
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
       </p>
       <h3>
        <strong>
         查询时一定要制定字段查询
        </strong>
       </h3>
       <p>
        l  查询时一定不能使用”*”来代替字段来进行查询，无论你查询的字段有多少个，就算字段太多无法走索引也避免了解析”*”带来的额外消耗。
       </p>
       <p>
        l  查询字段值列出想要的字段，避免出现多余的字段，字段越多查询开销越大而且可能会因为多列出了某个字段而引起查询不走索引。
       </p>
       <p>
        创建测试数据库
       </p>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d435a9644481760" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           CREATE TABLE [Sales].[Customer](
    [CustomerID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
    [PersonID] [int] NULL,
    [StoreID] [int] NULL,
    [TerritoryID] [int] NULL,
    [AccountNumber]  AS (isnull('AW'+[dbo].[ufnLeadingZeros]([CustomerID]),'')),
    [rowguid] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
    [ModifiedDate] [datetime] NOT NULL,
 CONSTRAINT [PK_Customer_CustomerID] PRIMARY KEY CLUSTERED 
(
    [CustomerID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d435a9644481760-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a9644481760-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435a9644481760-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a9644481760-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435a9644481760-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a9644481760-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435a9644481760-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a9644481760-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435a9644481760-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a9644481760-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435a9644481760-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435a9644481760-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435a9644481760-13">
                13
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d435a9644481760-1">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-i">
                 TABLE
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 Sales
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 Customer
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 (
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a9644481760-2">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 CustomerID
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 IDENTITY
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 FOR
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 REPLICATION
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435a9644481760-3">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 PersonID
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a9644481760-4">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 StoreID
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435a9644481760-5">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 TerritoryID
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a9644481760-6">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 AccountNumber
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 AS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 isnull
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 'AW'
                </span>
                <span class="crayon-o">
                 +
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 dbo
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 ufnLeadingZeros
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 CustomerID
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 ''
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435a9644481760-7">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 rowguid
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 uniqueidentifier
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 ROWGUIDCOL
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a9644481760-8">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 ModifiedDate
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 datetime
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435a9644481760-9">
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 CONSTRAINT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 PK_Customer_CustomerID
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 PRIMARY
                </span>
                <span class="crayon-e">
                 KEY
                </span>
                <span class="crayon-e">
                 CLUSTERED
                </span>
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a9644481760-10">
                <span class="crayon-sy">
                 (
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435a9644481760-11">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 CustomerID
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 ASC
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435a9644481760-12">
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-e">
                 WITH
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 PAD_INDEX
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 OFF
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 STATISTICS_NORECOMPUTE
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 OFF
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 IGNORE_DUP_KEY
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 OFF
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 ALLOW_ROW_LOCKS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 ON
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 ALLOW_PAGE_LOCKS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 ON
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 ON
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 PRIMARY
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435a9644481760-13">
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 ON
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 PRIMARY
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0124 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
        创建索引
       </p>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d435b2439115156" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           CREATE NONCLUSTERED INDEX [IX1_Customer] ON [Sales].[Customer] 
(
    [PersonID] ASC
)
INCLUDE ( [StoreID],
[TerritoryID],
[AccountNumber],
[rowguid]) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d435b2439115156-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435b2439115156-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435b2439115156-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435b2439115156-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435b2439115156-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435b2439115156-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435b2439115156-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435b2439115156-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435b2439115156-9">
                9
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d435b2439115156-1">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 NONCLUSTERED
                </span>
                <span class="crayon-i">
                 INDEX
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 IX1_Customer
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 ON
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 Sales
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 Customer
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435b2439115156-2">
                <span class="crayon-sy">
                 (
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435b2439115156-3">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 PersonID
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 ASC
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435b2439115156-4">
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435b2439115156-5">
                <span class="crayon-e">
                 INCLUDE
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 StoreID
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435b2439115156-6">
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 TerritoryID
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435b2439115156-7">
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 AccountNumber
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435b2439115156-8">
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 rowguid
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 WITH
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 PAD_INDEX
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 OFF
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 STATISTICS_NORECOMPUTE
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 OFF
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 SORT_IN_TEMPDB
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 OFF
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 IGNORE_DUP_KEY
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 OFF
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 DROP_EXISTING
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 OFF
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 ONLINE
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 OFF
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 ALLOW_ROW_LOCKS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 ON
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 ALLOW_PAGE_LOCKS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 ON
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 ON
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 PRIMARY
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435b2439115156-9">
                <span class="crayon-v">
                 GO
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0116 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
        查询测试
       </p>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d435b9808903151" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           ---使用SELECT  * 查询
SET STATISTICS IO ON
SET STATISTICS TIME ON
SELECT  *  FROM [Sales].[Customer]
WHERE PersonID=1;
 SET STATISTICS TIME OFF
 SET STATISTICS IO OFF
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d435b9808903151-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435b9808903151-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435b9808903151-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435b9808903151-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435b9808903151-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435b9808903151-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435b9808903151-7">
                7
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d435b9808903151-1">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                使用
                <span class="crayon-e ">
                 SELECT  *
                </span>
                <span class="crayon-h">
                </span>
                查询
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435b9808903151-2">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 IO
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435b9808903151-3">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 TIME
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435b9808903151-4">
                <span class="crayon-e ">
                 SELECT  *
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 FROM
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 Sales
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 Customer
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435b9808903151-5">
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-v">
                 PersonID
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435b9808903151-6">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 TIME
                </span>
                <span class="crayon-e">
                 OFF
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435b9808903151-7">
                <span class="crayon-e">
                </span>
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 IO
                </span>
                <span class="crayon-v">
                 OFF
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0019 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/291427552376540.png"/>
       </p>
       <p>
        由于建的索引‘IX1_Customer’没有包含ModifiedDate字段，所以需要通过键查找去聚集索引中获取该字段的值
       </p>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d435c0684089963" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           ---列出需要的字段查询，因为字段不包含不需要的列，所以走索引
SET STATISTICS IO ON
SET STATISTICS TIME ON  
  SELECT CustomerID, 
       [PersonID]
      ,[StoreID]
      ,[TerritoryID]
      ,[AccountNumber]
      ,[rowguid]
  FROM [Sales].[Customer]
  WHERE PersonID=1;
 SET STATISTICS TIME OFF
 SET STATISTICS IO OFF
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d435c0684089963-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c0684089963-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c0684089963-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c0684089963-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c0684089963-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c0684089963-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c0684089963-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c0684089963-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c0684089963-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c0684089963-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c0684089963-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c0684089963-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c0684089963-13">
                13
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d435c0684089963-1">
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                列出需要的字段查询，因为字段不包含不需要的列，所以走索引
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c0684089963-2">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 IO
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c0684089963-3">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 TIME
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c0684089963-4">
                <span class="crayon-e">
                </span>
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-v">
                 CustomerID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c0684089963-5">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 PersonID
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c0684089963-6">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 StoreID
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c0684089963-7">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 TerritoryID
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c0684089963-8">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 AccountNumber
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c0684089963-9">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 rowguid
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c0684089963-10">
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 FROM
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 Sales
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 Customer
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c0684089963-11">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-v">
                 PersonID
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c0684089963-12">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 TIME
                </span>
                <span class="crayon-e">
                 OFF
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c0684089963-13">
                <span class="crayon-e">
                </span>
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 IO
                </span>
                <span class="crayon-v">
                 OFF
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0031 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/291430391757991.png"/>
       </p>
       <p>
        由于查询语句中没有对ModifiedDate字段进行查询，所以只走索引查找就可以查询到需要的数据，所以建议在查询语句中列出你需要的字段而不是为了方便用*来查询所有的字段，如果真的
       </p>
       <p>
        需要查询所有的字段也同样建议把所有的字段列出来取代‘*’。
       </p>
       <p>
       </p>
       <h3>
        <strong>
         使用存储过程的好处
        </strong>
       </h3>
       <ol>
        <li>
         减少网络通信量。调用一个行数不多的存储过程与直接调用SQL语句的网络通信量可能不会有很大的差别，可是如果存储过程包含上百行SQL语句，那么其性能绝对比一条一条的调用SQL语句要高得多。
        </li>
        <li>
         执行速度更快。有两个原因：首先，在存储过程创建的时候，数据库已经对其进行了一次解析和优化。其次，存储过程一旦执行，在内存中就会保留一份这个存储过程缓存计划，这样下次再执行同样的存储过程时，可以从内存中直接调用。
        </li>
        <li>
         更强的适应性：由于存储过程对数据库的访问是通过存储过程来进行的，因此数据库开发人员可以在不改动存储过程接口的情况下对数据库进行任何改动，而这些改动不会对应用程序造成影响。
        </li>
        <li>
         布式工作：应用程序和数据库的编码工作可以分别独立进行，而不会相互压制。
        </li>
        <li>
         更好的封装移植性。
        </li>
        <li>
         安全性，它们可以防止某些类型的 SQL 插入攻击。
        </li>
       </ol>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d435c9535614586" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           PROCEDURE [dbo].[SPSalesPerson]
(@option varchar(50))
AS
BEGIN
SET NOCOUNT ON
IF @option='select'
    BEGIN
    SELECT [DatabaseLogID]
          ,[PostTime]
          ,[DatabaseUser]
          ,[Event]
          ,[Schema]
          ,[Object]
          ,[TSQL]
          ,[XmlEvent]
      FROM [dbo].[DatabaseLog]
      END
IF @option='SalesPerson'
   BEGIN
   SELECT [BusinessEntityID]
      ,[TerritoryID]
      ,[SalesQuota]
      ,[Bonus]
      ,[CommissionPct]
      ,[SalesYTD]
      ,[SalesLastYear]
      ,[rowguid]
      ,[ModifiedDate]
   FROM [Sales].[SalesPerson]
   WHERE BusinessEntityID&amp;lt;300
   END
SET NOCOUNT OFF  
END
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c9535614586-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c9535614586-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c9535614586-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c9535614586-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c9535614586-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c9535614586-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-13">
                13
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c9535614586-14">
                14
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-15">
                15
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c9535614586-16">
                16
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-17">
                17
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c9535614586-18">
                18
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-19">
                19
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c9535614586-20">
                20
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-21">
                21
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c9535614586-22">
                22
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-23">
                23
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c9535614586-24">
                24
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-25">
                25
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c9535614586-26">
                26
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-27">
                27
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c9535614586-28">
                28
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-29">
                29
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c9535614586-30">
                30
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-31">
                31
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435c9535614586-32">
                32
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435c9535614586-33">
                33
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d435c9535614586-1">
                <span class="crayon-i">
                 PROCEDURE
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 dbo
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 SPSalesPerson
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c9535614586-2">
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-e">
                 option
                </span>
                <span class="crayon-e">
                 varchar
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 50
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c9535614586-3">
                <span class="crayon-st">
                 AS
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c9535614586-4">
                <span class="crayon-e">
                 BEGIN
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c9535614586-5">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 NOCOUNT
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c9535614586-6">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-v">
                 option
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 'select'
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c9535614586-7">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 BEGIN
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c9535614586-8">
                <span class="crayon-e">
                </span>
                <span class="crayon-i">
                 SELECT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 DatabaseLogID
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c9535614586-9">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 PostTime
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c9535614586-10">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 DatabaseUser
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c9535614586-11">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 Event
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c9535614586-12">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 Schema
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c9535614586-13">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-t">
                 Object
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c9535614586-14">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 TSQL
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c9535614586-15">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 XmlEvent
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c9535614586-16">
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 FROM
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 dbo
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 DatabaseLog
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c9535614586-17">
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 END
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c9535614586-18">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-v">
                 option
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 'SalesPerson'
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c9535614586-19">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 BEGIN
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c9535614586-20">
                <span class="crayon-e">
                </span>
                <span class="crayon-i">
                 SELECT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 BusinessEntityID
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c9535614586-21">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 TerritoryID
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c9535614586-22">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 SalesQuota
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c9535614586-23">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 Bonus
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c9535614586-24">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 CommissionPct
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c9535614586-25">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 SalesYTD
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c9535614586-26">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 SalesLastYear
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c9535614586-27">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 rowguid
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c9535614586-28">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 ModifiedDate
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c9535614586-29">
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 FROM
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 Sales
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 SalesPerson
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c9535614586-30">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-v">
                 BusinessEntityID
                </span>
                <span class="crayon-o">
                 &amp;
                </span>
                <span class="crayon-v">
                 lt
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-cn">
                 300
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c9535614586-31">
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 END
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435c9535614586-32">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 NOCOUNT
                </span>
                <span class="crayon-e">
                 OFF
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435c9535614586-33">
                <span class="crayon-st">
                 END
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0121 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d435d2590097803" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           EXEC SPSalesPerson @option='select'
EXEC SPSalesPerson @option='SalesPerson'

DBCC FREEPROCCACHE----清空缓存

---测试两个查询是否都走了缓存计划
SELECT usecounts,size_in_bytes,cacheobjtype,objtype,TEXT FROM  sys.dm_exec_cached_plans  cp  
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st;

--执行计划在第一次执行SQL语句时产生，缓存在内存中，这个缓存的计划一直可用，直到 SQL Server 重新启动，或直到它由于使用率较低而溢出内存。

默认情况下，存储过程将返回过程中每个语句影响的行数。如果不需要在应用程序中使用该信息（大多数应用程序并不需要），请在存储过程中使用 SET NOCOUNT ON 语句以终止该行为。根据存储过程中包含的影响行的语句的数量，这将删除客户端和服务器之间的一个或多个往返过程。尽管这不是大问题，但它可以为高流量应用程序的性能产生负面影响。
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d435d2590097803-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435d2590097803-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435d2590097803-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435d2590097803-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435d2590097803-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435d2590097803-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435d2590097803-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435d2590097803-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435d2590097803-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435d2590097803-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435d2590097803-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435d2590097803-12">
                12
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d435d2590097803-1">
                <span class="crayon-e">
                 EXEC
                </span>
                <span class="crayon-i">
                 SPSalesPerson
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-v">
                 option
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 'select'
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435d2590097803-2">
                <span class="crayon-e">
                 EXEC
                </span>
                <span class="crayon-i">
                 SPSalesPerson
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-v">
                 option
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 'SalesPerson'
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435d2590097803-3">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435d2590097803-4">
                <span class="crayon-e">
                 DBCC
                </span>
                <span class="crayon-v">
                 FREEPROCCACHE
                </span>
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 --
                </span>
                清空缓存
               </div>
               <div class="crayon-line" id="crayon-5768534d435d2590097803-5">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435d2590097803-6">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                测试两个查询是否都走了缓存计划
               </div>
               <div class="crayon-line" id="crayon-5768534d435d2590097803-7">
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-v">
                 usecounts
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 size_in_bytes
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 cacheobjtype
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 objtype
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-e">
                 TEXT
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-v">
                 sys
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 dm_exec_cached_plans
                </span>
                <span class="crayon-e">
                 cp
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435d2590097803-8">
                <span class="crayon-e">
                 CROSS
                </span>
                <span class="crayon-e">
                 APPLY
                </span>
                <span class="crayon-v">
                 sys
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 dm_exec_sql_text
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 cp
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 plan_handle
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 st
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435d2590097803-9">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435d2590097803-10">
                <span class="crayon-o">
                 --
                </span>
                执行计划在第一次执行
                <span class="crayon-i">
                 SQL
                </span>
                语句时产生，缓存在内存中，这个缓存的计划一直可用，直到
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 SQL
                </span>
                <span class="crayon-i">
                 Server
                </span>
                <span class="crayon-h">
                </span>
                重新启动，或直到它由于使用率较低而溢出内存。
               </div>
               <div class="crayon-line" id="crayon-5768534d435d2590097803-11">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435d2590097803-12">
                默认情况下，存储过程将返回过程中每个语句影响的行数。如果不需要在应用程序中使用该信息（大多数应用程序并不需要），请在存储过程中使用
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 NOCOUNT
                </span>
                <span class="crayon-i">
                 ON
                </span>
                <span class="crayon-h">
                </span>
                语句以终止该行为。根据存储过程中包含的影响行的语句的数量，这将删除客户端和服务器之间的一个或多个往返过程。尽管这不是大问题，但它可以为高流量应用程序的性能产生负面影响。
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0091 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <h3>
        判断一条查询是否有值
       </h3>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d435da915993823" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           --以下四个查询都是判断连接查询无记录时所做的操作
---性能最差消耗0.8秒
SET STATISTICS IO ON 
SET STATISTICS TIME ON
DECLARE @UserType INT ,@Status INT
SELECT  @UserType=COUNT(c.Id) FROM Customerfo t INNER JOIN Customer c ON c.Id=t.CustomerId    
        WHERE c.customerTel='13400000000'
            IF(@UserType=0)
            BEGIN
                SET @Status = 2      
                    PRINT  @Status
            END
SET STATISTICS TIME OFF    
SET STATISTICS IO OFF            
    go    

----性能较好消耗0.08秒    
SET STATISTICS IO ON 
SET STATISTICS TIME ON
        
IF NOT EXISTS(SELECT c.Id FROM Customerfo t INNER JOIN Customer c ON c.Id=t.CustomerId WHERE c.customerTel='13400000000')
    BEGIN
    DECLARE @Status int
     SET @Status = 2 
    PRINT @Status
    END
        
SET STATISTICS TIME OFF    
SET STATISTICS IO OFF
    go    

----性能较好消耗0.08秒            
SET STATISTICS IO ON 
SET STATISTICS TIME ON
        
IF NOT EXISTS(SELECT top 1 c.id FROM Customerfo t INNER JOIN Customer c ON c.Id=t.CustomerId WHERE c.customerTel='13400000000'
        ORDER BY NEWID() )
    BEGIN
    DECLARE @Status int
     SET @Status = 2 
    PRINT @Status
    END
        
SET STATISTICS TIME OFF    
SET STATISTICS IO OFF    
            
GO

---性能和上面的一样0.08秒
SET STATISTICS IO ON 
SET STATISTICS TIME ON
        
    IF NOT EXISTS(SELECT 1  FROM Customerfo t INNER JOIN Customer c ON c.Id=t.CustomerId WHERE c.customerTel='13410700660' )
    BEGIN
    DECLARE @Status int
     SET @Status = 2 
    PRINT @Status
    END
        
SET STATISTICS TIME OFF    
SET STATISTICS IO OFF    

这里说一下SELECT 1,之前因为有程序员误认为查询SELECT 1无论查询的数据有多少只返回一个1，其实不是这样的，和查询字段是一样的意思只是有多少记录就返回多少个1,1也不是查询的第一个字段。
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-13">
                13
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-14">
                14
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-15">
                15
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-16">
                16
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-17">
                17
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-18">
                18
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-19">
                19
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-20">
                20
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-21">
                21
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-22">
                22
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-23">
                23
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-24">
                24
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-25">
                25
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-26">
                26
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-27">
                27
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-28">
                28
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-29">
                29
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-30">
                30
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-31">
                31
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-32">
                32
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-33">
                33
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-34">
                34
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-35">
                35
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-36">
                36
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-37">
                37
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-38">
                38
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-39">
                39
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-40">
                40
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-41">
                41
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-42">
                42
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-43">
                43
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-44">
                44
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-45">
                45
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-46">
                46
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-47">
                47
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-48">
                48
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-49">
                49
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-50">
                50
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-51">
                51
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-52">
                52
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-53">
                53
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-54">
                54
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-55">
                55
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-56">
                56
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-57">
                57
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-58">
                58
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-59">
                59
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-60">
                60
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-61">
                61
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435da915993823-62">
                62
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435da915993823-63">
                63
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d435da915993823-1">
                <span class="crayon-o">
                 --
                </span>
                以下四个查询都是判断连接查询无记录时所做的操作
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-2">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                性能最差消耗
                <span class="crayon-cn">
                 0.8
                </span>
                秒
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-3">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 IO
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-4">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 TIME
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-5">
                <span class="crayon-r">
                 DECLARE
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-e">
                 UserType
                </span>
                <span class="crayon-t">
                 INT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-e">
                 Status
                </span>
                <span class="crayon-t">
                 INT
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-6">
                <span class="crayon-i">
                 SELECT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-v">
                 UserType
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-e">
                 COUNT
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 c
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 Id
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-i">
                 Customerfo
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 t
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 INNER
                </span>
                <span class="crayon-e">
                 JOIN
                </span>
                <span class="crayon-i">
                 Customer
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 c
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 ON
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 c
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 Id
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-v">
                 t
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 CustomerId
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-7">
                <span class="crayon-e">
                </span>
                <span class="crayon-i">
                 WHERE
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 c
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 customerTel
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 '13400000000'
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-8">
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-v">
                 UserType
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 0
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-9">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 BEGIN
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-10">
                <span class="crayon-e">
                </span>
                <span class="crayon-i">
                 SET
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-v">
                 Status
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-11">
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 PRINT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-e">
                 Status
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-12">
                <span class="crayon-e">
                </span>
                <span class="crayon-st">
                 END
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-13">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 TIME
                </span>
                <span class="crayon-e">
                 OFF
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-14">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 IO
                </span>
                <span class="crayon-e">
                 OFF
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-15">
                <span class="crayon-e">
                </span>
                <span class="crayon-v">
                 go
                </span>
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-16">
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-17">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 --
                </span>
                性能较好消耗
                <span class="crayon-cn">
                 0.08
                </span>
                秒
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-18">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 IO
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-19">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 TIME
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-20">
                <span class="crayon-e">
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-21">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 EXISTS
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-i">
                 SELECT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 c
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Id
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-i">
                 Customerfo
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 t
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 INNER
                </span>
                <span class="crayon-e">
                 JOIN
                </span>
                <span class="crayon-i">
                 Customer
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 c
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 ON
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 c
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 Id
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-v">
                 t
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 CustomerId
                </span>
                <span class="crayon-i">
                 WHERE
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 c
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 customerTel
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 '13400000000'
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-22">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 BEGIN
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-23">
                <span class="crayon-e">
                </span>
                <span class="crayon-r">
                 DECLARE
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-e">
                 Status
                </span>
                <span class="crayon-t">
                 int
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-24">
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 SET
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-v">
                 Status
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-25">
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 PRINT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-e">
                 Status
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-26">
                <span class="crayon-e">
                </span>
                <span class="crayon-st">
                 END
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-27">
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-28">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 TIME
                </span>
                <span class="crayon-e">
                 OFF
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-29">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 IO
                </span>
                <span class="crayon-e">
                 OFF
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-30">
                <span class="crayon-e">
                </span>
                <span class="crayon-v">
                 go
                </span>
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-31">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-32">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 --
                </span>
                性能较好消耗
                <span class="crayon-cn">
                 0.08
                </span>
                秒
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-33">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 IO
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-34">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 TIME
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-35">
                <span class="crayon-e">
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-36">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 EXISTS
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-i">
                 top
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 c
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 id
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-i">
                 Customerfo
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 t
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 INNER
                </span>
                <span class="crayon-e">
                 JOIN
                </span>
                <span class="crayon-i">
                 Customer
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 c
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 ON
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 c
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 Id
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-v">
                 t
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 CustomerId
                </span>
                <span class="crayon-i">
                 WHERE
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 c
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 customerTel
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 '13400000000'
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-37">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 ORDER
                </span>
                <span class="crayon-e">
                 BY
                </span>
                <span class="crayon-e">
                 NEWID
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-38">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 BEGIN
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-39">
                <span class="crayon-e">
                </span>
                <span class="crayon-r">
                 DECLARE
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-e">
                 Status
                </span>
                <span class="crayon-t">
                 int
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-40">
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 SET
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-v">
                 Status
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-41">
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 PRINT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-e">
                 Status
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-42">
                <span class="crayon-e">
                </span>
                <span class="crayon-st">
                 END
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-43">
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-44">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 TIME
                </span>
                <span class="crayon-e">
                 OFF
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-45">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 IO
                </span>
                <span class="crayon-e">
                 OFF
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-46">
                <span class="crayon-e">
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-47">
                <span class="crayon-v">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-48">
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-49">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                性能和上面的一样
                <span class="crayon-cn">
                 0.08
                </span>
                秒
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-50">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 IO
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-51">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 TIME
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-52">
                <span class="crayon-e">
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-53">
                <span class="crayon-e">
                </span>
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 EXISTS
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-i">
                 SELECT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-i">
                 Customerfo
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 t
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 INNER
                </span>
                <span class="crayon-e">
                 JOIN
                </span>
                <span class="crayon-i">
                 Customer
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 c
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 ON
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 c
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 Id
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-v">
                 t
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 CustomerId
                </span>
                <span class="crayon-i">
                 WHERE
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 c
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 customerTel
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 '13410700660'
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-54">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 BEGIN
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-55">
                <span class="crayon-e">
                </span>
                <span class="crayon-r">
                 DECLARE
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-e">
                 Status
                </span>
                <span class="crayon-t">
                 int
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-56">
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 SET
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-v">
                 Status
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-57">
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 PRINT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-e">
                 Status
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-58">
                <span class="crayon-e">
                </span>
                <span class="crayon-st">
                 END
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-59">
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-60">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 TIME
                </span>
                <span class="crayon-e">
                 OFF
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-61">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 STATISTICS
                </span>
                <span class="crayon-e">
                 IO
                </span>
                <span class="crayon-i">
                 OFF
                </span>
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435da915993823-62">
               </div>
               <div class="crayon-line" id="crayon-5768534d435da915993823-63">
                这里说一下
                <span class="crayon-i">
                 SELECT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                之前因为有程序员误认为查询
                <span class="crayon-i">
                 SELECT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-cn">
                 1
                </span>
                无论查询的数据有多少只返回一个
                <span class="crayon-cn">
                 1
                </span>
                ，其实不是这样的，和查询字段是一样的意思只是有多少记录就返回多少个
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 1
                </span>
                也不是查询的第一个字段。
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0289 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
        <strong>
         理解TRUNCATE和DELETE的区别
        </strong>
       </p>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d435e2227926864" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           ---创建表Table1
IF OBJECT_ID('Table1','U') IS NOT NULL
DROP TABLE Table1
GO
CREATE TABLE Table1
(ID INT NOT NULL,
FOID INT NOT NULL)
GO

--插入测试数据
INSERT INTO Table1
VALUES(1,101),(2,102),(3,103),(4,104)
GO

---创建表Table2
IF OBJECT_ID('Table2','U') IS NOT NULL
DROP TABLE Table2
GO
CREATE TABLE Table2
(
FOID INT NOT NULL)
GO
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d435e2227926864-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435e2227926864-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435e2227926864-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435e2227926864-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435e2227926864-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435e2227926864-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435e2227926864-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435e2227926864-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435e2227926864-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435e2227926864-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435e2227926864-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435e2227926864-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435e2227926864-13">
                13
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435e2227926864-14">
                14
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435e2227926864-15">
                15
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435e2227926864-16">
                16
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435e2227926864-17">
                17
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435e2227926864-18">
                18
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435e2227926864-19">
                19
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435e2227926864-20">
                20
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435e2227926864-21">
                21
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435e2227926864-22">
                22
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d435e2227926864-1">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                创建表
                <span class="crayon-e">
                 Table1
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435e2227926864-2">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 OBJECT_ID
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 'Table1'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 'U'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 IS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435e2227926864-3">
                <span class="crayon-e">
                 DROP
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-e">
                 Table1
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435e2227926864-4">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435e2227926864-5">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-e">
                 Table1
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435e2227926864-6">
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 ID
                </span>
                <span class="crayon-t">
                 INT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435e2227926864-7">
                <span class="crayon-e">
                 FOID
                </span>
                <span class="crayon-t">
                 INT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435e2227926864-8">
                <span class="crayon-v">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435e2227926864-9">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435e2227926864-10">
                <span class="crayon-o">
                 --
                </span>
                插入测试数据
               </div>
               <div class="crayon-line" id="crayon-5768534d435e2227926864-11">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Table1
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435e2227926864-12">
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 101
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 102
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 3
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 103
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 4
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 104
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435e2227926864-13">
                <span class="crayon-v">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435e2227926864-14">
               </div>
               <div class="crayon-line" id="crayon-5768534d435e2227926864-15">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                创建表
                <span class="crayon-e">
                 Table2
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435e2227926864-16">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 OBJECT_ID
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 'Table2'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 'U'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 IS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435e2227926864-17">
                <span class="crayon-e">
                 DROP
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-e">
                 Table2
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435e2227926864-18">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435e2227926864-19">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-e">
                 Table2
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435e2227926864-20">
                <span class="crayon-sy">
                 (
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435e2227926864-21">
                <span class="crayon-e">
                 FOID
                </span>
                <span class="crayon-t">
                 INT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435e2227926864-22">
                <span class="crayon-v">
                 GO
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0103 seconds] -->
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d435e8387027017" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           --插入测试数据
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d435e8387027017-1">
                1
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d435e8387027017-1">
                <span class="crayon-o">
                 --
                </span>
                插入测试数据
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0005 seconds] -->
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d435ee188546174" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           INSERT INTO Table2 VALUES(101),(102),(103),(104)
GO 
SELECT * FROM Table1
GO 
SELECT * FROM Table2
GO
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d435ee188546174-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435ee188546174-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435ee188546174-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435ee188546174-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d435ee188546174-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435ee188546174-6">
                6
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d435ee188546174-1">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Table2
                </span>
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 101
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 102
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 103
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 104
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435ee188546174-2">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435ee188546174-3">
                <span class="crayon-e ">
                 SELECT *
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 Table1
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435ee188546174-4">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d435ee188546174-5">
                <span class="crayon-e ">
                 SELECT *
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 Table2
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d435ee188546174-6">
                <span class="crayon-v">
                 GO
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0093 seconds] -->
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d435f3278552972" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           在Table1表中创建触发器,当表中的数据被删除时同时删除Table2表中对应的FOID
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d435f3278552972-1">
                1
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d435f3278552972-1">
                在
                <span class="crayon-i">
                 Table1
                </span>
                表中创建触发器
                <span class="crayon-sy">
                 ,
                </span>
                当表中的数据被删除时同时删除
                <span class="crayon-i">
                 Table2
                </span>
                表中对应的
                <span class="crayon-v">
                 FOID
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0010 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code">
         <div class="cnblogs_code_toolbar">
          <span class="cnblogs_code_copy">
           <p title="复制代码">
            <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
           </p>
          </span>
         </div>
         <p>
         </p>
         <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
         <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d435f9111786443" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
          <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <span class="crayon-title">
           </span>
           <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
            <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-wrap-button" title="切换自动换行">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-expand-button" title="点击展开代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-copy-button" title="复制代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
           </div>
          </div>
          <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
          </div>
          <div class="crayon-plain-wrap">
           <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
            CREATE TRIGGER TG_Table1 ON Table1
AFTER DELETE
AS
BEGIN
  DELETE FROM TA FROM Table2 TA INNER JOIN deleted TB ON TA.FOID=TB.FOID 
END
GO
           </textarea>
          </div>
          <div class="crayon-main" style="">
           <table class="crayon-table">
            <tbody>
             <tr class="crayon-row">
              <td class="crayon-nums " data-settings="show">
               <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
                <div class="crayon-num" data-line="crayon-5768534d435f9111786443-1">
                 1
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435f9111786443-2">
                 2
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435f9111786443-3">
                 3
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435f9111786443-4">
                 4
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435f9111786443-5">
                 5
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d435f9111786443-6">
                 6
                </div>
                <div class="crayon-num" data-line="crayon-5768534d435f9111786443-7">
                 7
                </div>
               </div>
              </td>
              <td class="crayon-code">
               <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
                <div class="crayon-line" id="crayon-5768534d435f9111786443-1">
                 <span class="crayon-e">
                  CREATE
                 </span>
                 <span class="crayon-e">
                  TRIGGER
                 </span>
                 <span class="crayon-e">
                  TG_Table1
                 </span>
                 <span class="crayon-e">
                  ON
                 </span>
                 <span class="crayon-e">
                  Table1
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435f9111786443-2">
                 <span class="crayon-e">
                  AFTER
                 </span>
                 <span class="crayon-e">
                  DELETE
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435f9111786443-3">
                 <span class="crayon-st">
                  AS
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435f9111786443-4">
                 <span class="crayon-e">
                  BEGIN
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435f9111786443-5">
                 <span class="crayon-e">
                 </span>
                 <span class="crayon-e">
                  DELETE
                 </span>
                 <span class="crayon-e">
                  FROM
                 </span>
                 <span class="crayon-e">
                  TA
                 </span>
                 <span class="crayon-e">
                  FROM
                 </span>
                 <span class="crayon-e">
                  Table2
                 </span>
                 <span class="crayon-e">
                  TA
                 </span>
                 <span class="crayon-e">
                  INNER
                 </span>
                 <span class="crayon-e">
                  JOIN
                 </span>
                 <span class="crayon-e">
                  deleted
                 </span>
                 <span class="crayon-e">
                  TB
                 </span>
                 <span class="crayon-e">
                  ON
                 </span>
                 <span class="crayon-v">
                  TA
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  FOID
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-v">
                  TB
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-e">
                  FOID
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d435f9111786443-6">
                 <span class="crayon-st">
                  END
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d435f9111786443-7">
                 <span class="crayon-v">
                  GO
                 </span>
                </div>
               </div>
              </td>
             </tr>
            </tbody>
           </table>
          </div>
         </div>
         <!-- [Format Time: 0.0093 seconds] -->
         <p>
         </p>
         <div class="cnblogs_code_toolbar">
          <span class="cnblogs_code_copy">
           <p title="复制代码">
            <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
           </p>
          </span>
         </div>
        </div>
        <p>
        </p>
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d435ff009540294" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d435ff009540294-1">
                1
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d435ff009540294-1">
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <p>
        </p>
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43605396225902" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43605396225902-1">
                1
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43605396225902-1">
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <p>
        </p>
        <div class="cnblogs_code">
         <div class="cnblogs_code_toolbar">
          <span class="cnblogs_code_copy">
           <p title="复制代码">
            <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
           </p>
          </span>
         </div>
         <p>
         </p>
         <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
         <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d4360a759917916" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
          <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <span class="crayon-title">
           </span>
           <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
            <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-wrap-button" title="切换自动换行">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-expand-button" title="点击展开代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-copy-button" title="复制代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
           </div>
          </div>
          <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
          </div>
          <div class="crayon-plain-wrap">
           <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
            ---测试DELETE删除操作
DELETE FROM Table1 WHERE ID=1

GO
---执行触发器成功,Table2表中的FOID=101的数据也被删除
SELECT * FROM Table1
GO
SELECT * FROM Table2
           </textarea>
          </div>
          <div class="crayon-main" style="">
           <table class="crayon-table">
            <tbody>
             <tr class="crayon-row">
              <td class="crayon-nums " data-settings="show">
               <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
                <div class="crayon-num" data-line="crayon-5768534d4360a759917916-1">
                 1
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4360a759917916-2">
                 2
                </div>
                <div class="crayon-num" data-line="crayon-5768534d4360a759917916-3">
                 3
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4360a759917916-4">
                 4
                </div>
                <div class="crayon-num" data-line="crayon-5768534d4360a759917916-5">
                 5
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4360a759917916-6">
                 6
                </div>
                <div class="crayon-num" data-line="crayon-5768534d4360a759917916-7">
                 7
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4360a759917916-8">
                 8
                </div>
               </div>
              </td>
              <td class="crayon-code">
               <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
                <div class="crayon-line" id="crayon-5768534d4360a759917916-1">
                 <span class="crayon-o">
                  --
                 </span>
                 <span class="crayon-o">
                  -
                 </span>
                 测试
                 <span class="crayon-i">
                  DELETE
                 </span>
                 删除操作
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d4360a759917916-2">
                 <span class="crayon-e">
                  DELETE
                 </span>
                 <span class="crayon-e">
                  FROM
                 </span>
                 <span class="crayon-e">
                  Table1
                 </span>
                 <span class="crayon-e">
                  WHERE
                 </span>
                 <span class="crayon-v">
                  ID
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-cn">
                  1
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d4360a759917916-3">
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d4360a759917916-4">
                 <span class="crayon-v">
                  GO
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d4360a759917916-5">
                 <span class="crayon-o">
                  --
                 </span>
                 <span class="crayon-o">
                  -
                 </span>
                 执行触发器成功
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-i">
                  Table2
                 </span>
                 表中的
                 <span class="crayon-v">
                  FOID
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-cn">
                  101
                 </span>
                 的数据也被删除
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d4360a759917916-6">
                 <span class="crayon-e ">
                  SELECT *
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-e">
                  FROM
                 </span>
                 <span class="crayon-e">
                  Table1
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d4360a759917916-7">
                 <span class="crayon-e">
                  GO
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d4360a759917916-8">
                 <span class="crayon-e ">
                  SELECT *
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-e">
                  FROM
                 </span>
                 <span class="crayon-v">
                  Table2
                 </span>
                </div>
               </div>
              </td>
             </tr>
            </tbody>
           </table>
          </div>
         </div>
         <!-- [Format Time: 0.0017 seconds] -->
         <p>
         </p>
         <div class="cnblogs_code_toolbar">
          <span class="cnblogs_code_copy">
           <p title="复制代码">
            <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
           </p>
          </span>
         </div>
        </div>
        <p>
         <img src="http://dataunion.org/wp-content/uploads/2015/05/081504449529090.png"/>
        </p>
        <div class="cnblogs_code">
         <div class="cnblogs_code_toolbar">
          <span class="cnblogs_code_copy">
           <p title="复制代码">
            <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
           </p>
          </span>
         </div>
         <p>
         </p>
         <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
         <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43610734736261" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
          <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <span class="crayon-title">
           </span>
           <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
            <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-wrap-button" title="切换自动换行">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-expand-button" title="点击展开代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-copy-button" title="复制代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
           </div>
          </div>
          <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
          </div>
          <div class="crayon-plain-wrap">
           <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
            ---测试TRUNCATE删除操作
TRUNCATE TABLE Table1

GO
---Table2中的数据没有被删除
SELECT * FROM Table1
GO
SELECT * FROM Table2
           </textarea>
          </div>
          <div class="crayon-main" style="">
           <table class="crayon-table">
            <tbody>
             <tr class="crayon-row">
              <td class="crayon-nums " data-settings="show">
               <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
                <div class="crayon-num" data-line="crayon-5768534d43610734736261-1">
                 1
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43610734736261-2">
                 2
                </div>
                <div class="crayon-num" data-line="crayon-5768534d43610734736261-3">
                 3
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43610734736261-4">
                 4
                </div>
                <div class="crayon-num" data-line="crayon-5768534d43610734736261-5">
                 5
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43610734736261-6">
                 6
                </div>
                <div class="crayon-num" data-line="crayon-5768534d43610734736261-7">
                 7
                </div>
                <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43610734736261-8">
                 8
                </div>
               </div>
              </td>
              <td class="crayon-code">
               <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
                <div class="crayon-line" id="crayon-5768534d43610734736261-1">
                 <span class="crayon-o">
                  --
                 </span>
                 <span class="crayon-o">
                  -
                 </span>
                 测试
                 <span class="crayon-i">
                  TRUNCATE
                 </span>
                 删除操作
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d43610734736261-2">
                 <span class="crayon-e">
                  TRUNCATE
                 </span>
                 <span class="crayon-e">
                  TABLE
                 </span>
                 <span class="crayon-e">
                  Table1
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d43610734736261-3">
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d43610734736261-4">
                 <span class="crayon-v">
                  GO
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d43610734736261-5">
                 <span class="crayon-o">
                  --
                 </span>
                 <span class="crayon-o">
                  -
                 </span>
                 <span class="crayon-i">
                  Table2
                 </span>
                 中的数据没有被删除
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d43610734736261-6">
                 <span class="crayon-e ">
                  SELECT *
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-e">
                  FROM
                 </span>
                 <span class="crayon-e">
                  Table1
                 </span>
                </div>
                <div class="crayon-line" id="crayon-5768534d43610734736261-7">
                 <span class="crayon-e">
                  GO
                 </span>
                </div>
                <div class="crayon-line crayon-striped-line" id="crayon-5768534d43610734736261-8">
                 <span class="crayon-e ">
                  SELECT *
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-e">
                  FROM
                 </span>
                 <span class="crayon-v">
                  Table2
                 </span>
                </div>
               </div>
              </td>
             </tr>
            </tbody>
           </table>
          </div>
         </div>
         <!-- [Format Time: 0.0015 seconds] -->
         <p>
         </p>
         <div class="cnblogs_code_toolbar">
          <span class="cnblogs_code_copy">
           <p title="复制代码">
            <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
           </p>
          </span>
         </div>
        </div>
        <p>
         <img src="http://dataunion.org/wp-content/uploads/2015/05/081505464527679.png"/>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43617700764161" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           ---查看TRUNCATE和DELETE的日志记录情况
CHECKPOINT
GO
SELECT * FROM fn_dblog(NULL,NULL)
GO
DELETE FROM Table2
WHERE FOID=102
GO
SELECT * FROM fn_dblog(NULL,NULL)
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43617700764161-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43617700764161-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43617700764161-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43617700764161-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43617700764161-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43617700764161-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43617700764161-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43617700764161-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43617700764161-9">
                9
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43617700764161-1">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                查看
                <span class="crayon-i">
                 TRUNCATE
                </span>
                和
                <span class="crayon-i">
                 DELETE
                </span>
                的日志记录情况
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43617700764161-2">
                <span class="crayon-e">
                 CHECKPOINT
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43617700764161-3">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43617700764161-4">
                <span class="crayon-e ">
                 SELECT *
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 fn_dblog
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43617700764161-5">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43617700764161-6">
                <span class="crayon-e">
                 DELETE
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 Table2
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43617700764161-7">
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-v">
                 FOID
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 102
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43617700764161-8">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43617700764161-9">
                <span class="crayon-e ">
                 SELECT *
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 fn_dblog
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0129 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/081545437339460.png"/>
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d4361d660848990" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          在第四行记录有一个lop_delete_rows,lcx_heap的删除操作日志记录
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-5768534d4361d660848990-1">
               1
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-5768534d4361d660848990-1">
               <span class="crayon-h">
               </span>
               在第四行记录有一个
               <span class="crayon-v">
                lop_delete_rows
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                lcx
               </span>
               <span class="crayon-sy">
                _
               </span>
               heap的删除操作日志记录
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0010 seconds] -->
       <p>
       </p>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43622227896696" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           ----TRUNCATE日志记录
CHECKPOINT
GO
SELECT * FROM fn_dblog(NULL,NULL)
GO
TRUNCATE TABLE Table2
GO
SELECT * FROM fn_dblog(NULL,NULL)
GO
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43622227896696-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43622227896696-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43622227896696-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43622227896696-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43622227896696-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43622227896696-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43622227896696-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43622227896696-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43622227896696-9">
                9
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43622227896696-1">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-i">
                 TRUNCATE
                </span>
                日志记录
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43622227896696-2">
                <span class="crayon-e">
                 CHECKPOINT
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43622227896696-3">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43622227896696-4">
                <span class="crayon-e ">
                 SELECT *
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 fn_dblog
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43622227896696-5">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43622227896696-6">
                <span class="crayon-e">
                 TRUNCATE
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-e">
                 Table2
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43622227896696-7">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43622227896696-8">
                <span class="crayon-e ">
                 SELECT *
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 fn_dblog
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43622227896696-9">
                <span class="crayon-v">
                 GO
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0017 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/081543269215631.png"/>
       </p>
       <p>
        TRUNCATE操作没有记录删除日志操作
       </p>
       <p>
        主要的原因是因为TRUNCATE操作不会激活触发器,因为TRUNCATE操作不会记录各行的日志删除操作,所以当你需要删除一张表的数据时你需要考虑是否应该如有记录日志删除操作，而不是根据个人的习惯来操作。
       </p>
       <p>
       </p>
       <p>
        <strong>
         事务的理解
        </strong>
       </p>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43629122088208" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           ---创建表Table1
IF OBJECT_ID('Table1','U') IS NOT NULL
DROP TABLE Table1
GO
CREATE TABLE Table1
(ID INT NOT NULL PRIMARY KEY,
Age INT NOT NULL CHECK(Age&amp;gt;10 AND Age&amp;lt;50));
GO

---创建表Table2
IF OBJECT_ID('Table2','U') IS NOT NULL
DROP TABLE Table2
GO
CREATE TABLE Table2
(
ID INT NOT NULL)
GO
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43629122088208-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43629122088208-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43629122088208-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43629122088208-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43629122088208-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43629122088208-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43629122088208-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43629122088208-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43629122088208-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43629122088208-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43629122088208-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43629122088208-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43629122088208-13">
                13
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43629122088208-14">
                14
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43629122088208-15">
                15
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43629122088208-16">
                16
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43629122088208-17">
                17
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43629122088208-1">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                创建表
                <span class="crayon-e">
                 Table1
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43629122088208-2">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 OBJECT_ID
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 'Table1'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 'U'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 IS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43629122088208-3">
                <span class="crayon-e">
                 DROP
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-e">
                 Table1
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43629122088208-4">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43629122088208-5">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-e">
                 Table1
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43629122088208-6">
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 ID
                </span>
                <span class="crayon-t">
                 INT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 PRIMARY
                </span>
                <span class="crayon-v">
                 KEY
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43629122088208-7">
                <span class="crayon-e">
                 Age
                </span>
                <span class="crayon-t">
                 INT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 CHECK
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 Age
                </span>
                <span class="crayon-o">
                 &amp;
                </span>
                <span class="crayon-v">
                 gt
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-cn">
                 10
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 AND
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 Age
                </span>
                <span class="crayon-o">
                 &amp;
                </span>
                <span class="crayon-v">
                 lt
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-cn">
                 50
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43629122088208-8">
                <span class="crayon-v">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43629122088208-9">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43629122088208-10">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                创建表
                <span class="crayon-e">
                 Table2
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43629122088208-11">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 OBJECT_ID
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 'Table2'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 'U'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 IS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43629122088208-12">
                <span class="crayon-e">
                 DROP
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-e">
                 Table2
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43629122088208-13">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43629122088208-14">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-e">
                 Table2
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43629122088208-15">
                <span class="crayon-sy">
                 (
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43629122088208-16">
                <span class="crayon-e">
                 ID
                </span>
                <span class="crayon-t">
                 INT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43629122088208-17">
                <span class="crayon-v">
                 GO
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0189 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
        1.简单的事务提交
       </p>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43630189186656" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           BEGIN TRANSACTION
INSERT INTO Table1(ID,Age)
VALUES(1,20)
INSERT INTO Table1(ID,Age)
VALUES(2,5)
INSERT INTO Table1(ID,Age)
VALUES(2,20)
INSERT INTO Table1(ID,Age)
VALUES(3,20)
COMMIT TRANSACTION
GO
---第二条记录没有执行成功,其他的都执行成功
SELECT * FROM Table1
所以并不是事务中的任意一条语句报错整个事务都会回滚，其它的可执行成功的语句依然会执行成功并提交。
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43630189186656-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43630189186656-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43630189186656-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43630189186656-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43630189186656-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43630189186656-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43630189186656-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43630189186656-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43630189186656-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43630189186656-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43630189186656-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43630189186656-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43630189186656-13">
                13
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43630189186656-14">
                14
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43630189186656-1">
                <span class="crayon-e">
                 BEGIN
                </span>
                <span class="crayon-e">
                 TRANSACTION
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43630189186656-2">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Table1
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 ID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 Age
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43630189186656-3">
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 20
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43630189186656-4">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Table1
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 ID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 Age
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43630189186656-5">
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 5
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43630189186656-6">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Table1
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 ID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 Age
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43630189186656-7">
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 20
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43630189186656-8">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Table1
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 ID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 Age
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43630189186656-9">
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 3
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 20
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43630189186656-10">
                <span class="crayon-e">
                 COMMIT
                </span>
                <span class="crayon-e">
                 TRANSACTION
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43630189186656-11">
                <span class="crayon-v">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43630189186656-12">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                第二条记录没有执行成功
                <span class="crayon-sy">
                 ,
                </span>
                其他的都执行成功
               </div>
               <div class="crayon-line" id="crayon-5768534d43630189186656-13">
                <span class="crayon-e ">
                 SELECT *
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-i">
                 Table1
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43630189186656-14">
                所以并不是事务中的任意一条语句报错整个事务都会回滚，其它的可执行成功的语句依然会执行成功并提交。
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0224 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/090836344682569.png"/>
       </p>
       <p>
        2.TRY…CATCH
       </p>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43636519804186" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           DELETE FROM Table1

BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Table1(ID,Age)
VALUES(1,20)
INSERT INTO Table1(ID,Age)
VALUES(2,20)
INSERT INTO Table1(ID,Age)
VALUES(3,20)
INSERT INTO Table3
VALUES(1) 
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
END CATCH

----重新打开一个回话执行查询，发现由于存在对象出错BEGIN CATCH并没有收到执行报错，且事务一直处于打开状态，没有被提交，也没有执行回滚。
SELECT * FROM Table1

---如果事务已经提交查询XACT_STATE()的状态值是0,或者执行DBCC OPENTRAN
SELECT XACT_STATE()

DBCC OPENTRAN

---手动执行提交或者回滚操作
ROLLBACK TRANSACTION
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43636519804186-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43636519804186-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43636519804186-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43636519804186-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43636519804186-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43636519804186-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43636519804186-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43636519804186-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43636519804186-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43636519804186-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43636519804186-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43636519804186-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43636519804186-13">
                13
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43636519804186-14">
                14
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43636519804186-15">
                15
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43636519804186-16">
                16
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43636519804186-17">
                17
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43636519804186-18">
                18
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43636519804186-19">
                19
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43636519804186-20">
                20
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43636519804186-21">
                21
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43636519804186-22">
                22
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43636519804186-23">
                23
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43636519804186-24">
                24
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43636519804186-25">
                25
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43636519804186-26">
                26
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43636519804186-27">
                27
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43636519804186-28">
                28
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43636519804186-1">
                <span class="crayon-e">
                 DELETE
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 Table1
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43636519804186-2">
               </div>
               <div class="crayon-line" id="crayon-5768534d43636519804186-3">
                <span class="crayon-e">
                 BEGIN
                </span>
                <span class="crayon-st">
                 TRY
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43636519804186-4">
                <span class="crayon-e">
                 BEGIN
                </span>
                <span class="crayon-e">
                 TRANSACTION
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43636519804186-5">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Table1
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 ID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 Age
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43636519804186-6">
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 20
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43636519804186-7">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Table1
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 ID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 Age
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43636519804186-8">
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 20
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43636519804186-9">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Table1
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 ID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 Age
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43636519804186-10">
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 3
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 20
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43636519804186-11">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Table3
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43636519804186-12">
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43636519804186-13">
                <span class="crayon-e">
                 COMMIT
                </span>
                <span class="crayon-e">
                 TRANSACTION
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43636519804186-14">
                <span class="crayon-st">
                 END
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 TRY
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43636519804186-15">
                <span class="crayon-e">
                 BEGIN
                </span>
                <span class="crayon-st">
                 CATCH
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43636519804186-16">
                <span class="crayon-e">
                 ROLLBACK
                </span>
                <span class="crayon-e">
                 TRANSACTION
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43636519804186-17">
                <span class="crayon-st">
                 END
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 CATCH
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43636519804186-18">
               </div>
               <div class="crayon-line" id="crayon-5768534d43636519804186-19">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 --
                </span>
                重新打开一个回话执行查询，发现由于存在对象出错
                <span class="crayon-e">
                 BEGIN
                </span>
                <span class="crayon-st">
                 CATCH
                </span>
                并没有收到执行报错，且事务一直处于打开状态，没有被提交，也没有执行回滚。
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43636519804186-20">
                <span class="crayon-e ">
                 SELECT *
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-v">
                 Table1
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43636519804186-21">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43636519804186-22">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                如果事务已经提交查询
                <span class="crayon-e">
                 XACT_STATE
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                的状态值是
                <span class="crayon-cn">
                 0
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                或者执行
                <span class="crayon-e">
                 DBCC
                </span>
                <span class="crayon-e">
                 OPENTRAN
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43636519804186-23">
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-e">
                 XACT_STATE
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43636519804186-24">
               </div>
               <div class="crayon-line" id="crayon-5768534d43636519804186-25">
                <span class="crayon-e">
                 DBCC
                </span>
                <span class="crayon-v">
                 OPENTRAN
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43636519804186-26">
               </div>
               <div class="crayon-line" id="crayon-5768534d43636519804186-27">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                手动执行提交或者回滚操作
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43636519804186-28">
                <span class="crayon-e">
                 ROLLBACK
                </span>
                <span class="crayon-v">
                 TRANSACTION
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0131 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d4363d512918125" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          TRY...CATCH不会返回对象错误或者字段错误等类型的错误
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-5768534d4363d512918125-1">
               1
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-5768534d4363d512918125-1">
               <span class="crayon-st">
                TRY
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-st">
                CATCH
               </span>
               不会返回对象错误或者字段错误等类型的错误
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0009 seconds] -->
       <p>
        想详细了解TRY…CATCH请参考
        <a href="http://www.cnblogs.com/chenmh/articles/4012506.html" target="_blank">
         <u>
          http://www.cnblogs.com/chenmh/articles/4012506.html
         </u>
        </a>
       </p>
       <p>
       </p>
       <p>
        3.打开XACT_ABORT
       </p>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43643004756306" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           SET XACT_ABORT ON
BEGIN TRANSACTION
INSERT INTO Table1(ID,Age)
VALUES(1,20)
INSERT INTO Table1(ID,Age)
VALUES(2,20)
INSERT INTO Table1(ID,Age)
VALUES(3,20)
INSERT INTO Table3
VALUES(1) 
COMMIT TRANSACTION
SET XACT_ABORT OFF

---事务全部执行回滚操作(对象table3是不存在报错，但是也回滚所有的提交，跟上面的TRY...CATCH的区别)
SELECT * FROM Table1
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43643004756306-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43643004756306-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43643004756306-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43643004756306-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43643004756306-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43643004756306-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43643004756306-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43643004756306-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43643004756306-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43643004756306-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43643004756306-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43643004756306-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43643004756306-13">
                13
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43643004756306-14">
                14
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43643004756306-15">
                15
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43643004756306-1">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 XACT_ABORT
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43643004756306-2">
                <span class="crayon-e">
                 BEGIN
                </span>
                <span class="crayon-e">
                 TRANSACTION
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43643004756306-3">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Table1
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 ID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 Age
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43643004756306-4">
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 20
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43643004756306-5">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Table1
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 ID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 Age
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43643004756306-6">
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 20
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43643004756306-7">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Table1
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 ID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 Age
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43643004756306-8">
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 3
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 20
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43643004756306-9">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Table3
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43643004756306-10">
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43643004756306-11">
                <span class="crayon-e">
                 COMMIT
                </span>
                <span class="crayon-e">
                 TRANSACTION
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43643004756306-12">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 XACT_ABORT
                </span>
                <span class="crayon-v">
                 OFF
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43643004756306-13">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43643004756306-14">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                事务全部执行回滚操作
                <span class="crayon-sy">
                 (
                </span>
                对象
                <span class="crayon-i">
                 table3
                </span>
                是不存在报错，但是也回滚所有的提交，跟上面的
                <span class="crayon-st">
                 TRY
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-st">
                 CATCH
                </span>
                的区别
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43643004756306-15">
                <span class="crayon-e ">
                 SELECT *
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-v">
                 Table1
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0120 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/090858160933923.png"/>
       </p>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43649561667954" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           ---查询是否有打开事务
SELECT XACT_STATE()

DBCC OPENTRAN
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43649561667954-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43649561667954-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43649561667954-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43649561667954-4">
                4
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43649561667954-1">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                查询是否有打开事务
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43649561667954-2">
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-e">
                 XACT_STATE
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43649561667954-3">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43649561667954-4">
                <span class="crayon-e">
                 DBCC
                </span>
                <span class="crayon-v">
                 OPENTRAN
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0011 seconds] -->
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d4364f660880832" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           未查询到有打开事务
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d4364f660880832-1">
                1
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d4364f660880832-1">
                未查询到有打开事务
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0005 seconds] -->
        <p>
         当 SET XACT_ABORT 为 ON 时，如果执行 Transact-SQL 语句产生运行时错误，则整个事务将终止并回滚。
        </p>
        <p>
         当 SET XACT_ABORT 为 OFF 时，有时只回滚产生错误的 Transact-SQL 语句，而事务将继续进行处理。如果错误很严重，那么即使 SET XACT_ABORT 为 OFF，也可能回滚整个事务。OFF 是默认设置。
        </p>
        <p>
         编译错误（如语法错误）不受 SET XACT_ABORT 的影响。
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
        所以我们应该根据自己的需求选择正确的事务。
       </p>
       <p>
       </p>
       <h3>
        修改字段NOT NULL的过程
       </h3>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43655154033097" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           在Address表中的有一个Address字段，该字段允许为NULL，现在需要将其修改为NOT NULL.
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_Address
    (
    ID int NOT NULL,
    Address nvarchar(MAX) NOT NULL
    )  ON [PRIMARY]
     TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_Address SET (LOCK_ESCALATION = TABLE)
GO
IF EXISTS(SELECT * FROM dbo.Address)
     EXEC('INSERT INTO dbo.Tmp_Address (ID, Address)
        SELECT ID, Address FROM dbo.Address WITH (HOLDLOCK TABLOCKX)')
GO
DROP TABLE dbo.Address
GO
EXECUTE sp_rename N'dbo.Tmp_Address', N'Address', 'OBJECT' 
GO
COMMIT

---从上面就是一个重置字段为非空的过程，从上面的语句我们可以看到首先要创建一张临时表在临时表中Address字段建成了NOT NULL，然后将原表中的数据插入到临时表当中，最后修改表名，大家可以想一下如果我要修改的表有几千万数据，那这个过程该多么长而且内存一下子就会增加很多，所以大家建表的时候就要养成设字段为NOT NULL

--当你要向现有的表中增加一个字段的时候你也要不允许为NULL，可以用默认值替代空
Alter Table Address Add Type smallint Not Null Default (1)
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-13">
                13
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-14">
                14
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-15">
                15
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-16">
                16
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-17">
                17
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-18">
                18
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-19">
                19
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-20">
                20
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-21">
                21
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-22">
                22
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-23">
                23
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-24">
                24
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-25">
                25
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-26">
                26
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-27">
                27
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-28">
                28
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-29">
                29
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-30">
                30
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-31">
                31
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-32">
                32
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-33">
                33
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43655154033097-34">
                34
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43655154033097-35">
                35
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43655154033097-1">
                在
                <span class="crayon-i">
                 Address
                </span>
                表中的有一个
                <span class="crayon-i">
                 Address
                </span>
                字段，该字段允许为
                <span class="crayon-t">
                 NULL
                </span>
                ，现在需要将其修改为
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 .
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-2">
                <span class="crayon-e">
                 BEGIN
                </span>
                <span class="crayon-e">
                 TRANSACTION
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-3">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 QUOTED_IDENTIFIER
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-4">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 ARITHABORT
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-5">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 NUMERIC_ROUNDABORT
                </span>
                <span class="crayon-e">
                 OFF
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-6">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 CONCAT_NULL_YIELDS_NULL
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-7">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 ANSI_NULLS
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-8">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 ANSI_PADDING
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-9">
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-e">
                 ANSI_WARNINGS
                </span>
                <span class="crayon-e">
                 ON
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-10">
                <span class="crayon-e">
                 COMMIT
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-11">
                <span class="crayon-e">
                 BEGIN
                </span>
                <span class="crayon-e">
                 TRANSACTION
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-12">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-13">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 dbo
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Tmp_Address
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-14">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-15">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 ID
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-16">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 Address
                </span>
                <span class="crayon-e">
                 nvarchar
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 MAX
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-17">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 ON
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 PRIMARY
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-18">
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 TEXTIMAGE
                </span>
                <span class="crayon-sy">
                 _
                </span>
                ON
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 PRIMARY
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-19">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-20">
                <span class="crayon-e">
                 ALTER
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 dbo
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Tmp_Address
                </span>
                <span class="crayon-e">
                 SET
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 LOCK_ESCALATION
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 TABLE
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-21">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-22">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 EXISTS
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e ">
                 SELECT *
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-v">
                 dbo
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 Address
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-23">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 EXEC
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 'INSERT INTO dbo.Tmp_Address (ID, Address)
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-24">
                <span class="crayon-s">
                 SELECT ID, Address FROM dbo.Address WITH (HOLDLOCK TABLOCKX)'
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-25">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-26">
                <span class="crayon-e">
                 DROP
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 dbo
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Address
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-27">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-28">
                <span class="crayon-e">
                 EXECUTE
                </span>
                <span class="crayon-v">
                 sp
                </span>
                <span class="crayon-sy">
                 _
                </span>
                rename
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 N
                </span>
                <span class="crayon-s">
                 'dbo.Tmp_Address'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 N
                </span>
                <span class="crayon-s">
                 'Address'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-s">
                 'OBJECT'
                </span>
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-29">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-30">
                <span class="crayon-v">
                 COMMIT
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-31">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-32">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                从上面就是一个重置字段为非空的过程，从上面的语句我们可以看到首先要创建一张临时表在临时表中
                <span class="crayon-i">
                 Address
                </span>
                字段建成了
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                ，然后将原表中的数据插入到临时表当中，最后修改表名，大家可以想一下如果我要修改的表有几千万数据，那这个过程该多么长而且内存一下子就会增加很多，所以大家建表的时候就要养成设字段为
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-33">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43655154033097-34">
                <span class="crayon-o">
                 --
                </span>
                当你要向现有的表中增加一个字段的时候你也要不允许为
                <span class="crayon-t">
                 NULL
                </span>
                ，可以用默认值替代空
               </div>
               <div class="crayon-line" id="crayon-5768534d43655154033097-35">
                <span class="crayon-e">
                 Alter
                </span>
                <span class="crayon-e">
                 Table
                </span>
                <span class="crayon-e">
                 Address
                </span>
                <span class="crayon-e">
                 Add
                </span>
                <span class="crayon-e">
                 Type
                </span>
                <span class="crayon-e">
                 smallint
                </span>
                <span class="crayon-st">
                 Not
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 Null
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 Default
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0228 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <h3>
        条件字段的先后顺序
       </h3>
       <p>
        你平时在写T_SQL语句的时候WHERE条件后面的字段的先后顺序你有注意吗？
       </p>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d4365d557592274" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           ---创建测试表 
IF OBJECT_ID('TAINFO','U')IS NOT NULL DROP TABLE TAINFO
GO

CREATE TABLE [dbo].[TAINFO](
ID INT NOT NULL PRIMARY KEY IDENTITY(1,1),
OID INT NOT NULL,
Stats SMALLINT CHECK (Stats IN(1,2)),
MAC uniqueidentifier NOT NULL

) ON [PRIMARY]

GO
---插入测试数据
INSERT INTO TAINFO(OID,Stats,MAC)
 VALUES(101,1,'46B550F9-6E24-436D-9BC7-F0650F562E54'),(101,2,'46B550F9-6E24-436D-9BC7-F0650F562E54'),(102,1,'46B550F9-6E24-436D-9BC7-F0650F562E54'),
 (102,2,'46B550F9-6E24-436D-9BC7-F0650F562E54'),(103,2,'46B550F9-6E24-436D-9BC7-F0650F562E54'),(103,2,'46B550F9-6E24-436D-9BC7-F0650F562E54'),
 (103,1,'46B550F9-6E24-436D-9BC7-F0650F562E54'),(103,1,'46B550F9-6E24-436D-9BC7-F0650F562E54')
 GO
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d4365d557592274-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4365d557592274-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4365d557592274-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4365d557592274-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4365d557592274-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4365d557592274-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4365d557592274-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4365d557592274-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4365d557592274-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4365d557592274-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4365d557592274-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4365d557592274-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4365d557592274-13">
                13
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4365d557592274-14">
                14
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4365d557592274-15">
                15
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4365d557592274-16">
                16
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4365d557592274-17">
                17
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4365d557592274-18">
                18
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4365d557592274-19">
                19
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d4365d557592274-1">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                创建测试表
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4365d557592274-2">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 OBJECT_ID
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 'TAINFO'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 'U'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-st">
                 IS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 DROP
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-e">
                 TAINFO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4365d557592274-3">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4365d557592274-4">
               </div>
               <div class="crayon-line" id="crayon-5768534d4365d557592274-5">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-i">
                 TABLE
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 dbo
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 TAINFO
                </span>
                <span class="crayon-sy">
                 ]
                </span>
                <span class="crayon-sy">
                 (
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4365d557592274-6">
                <span class="crayon-e">
                 ID
                </span>
                <span class="crayon-t">
                 INT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 PRIMARY
                </span>
                <span class="crayon-e">
                 KEY
                </span>
                <span class="crayon-e">
                 IDENTITY
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4365d557592274-7">
                <span class="crayon-e">
                 OID
                </span>
                <span class="crayon-t">
                 INT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4365d557592274-8">
                <span class="crayon-e">
                 Stats
                </span>
                <span class="crayon-e">
                 SMALLINT
                </span>
                <span class="crayon-e">
                 CHECK
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 Stats
                </span>
                <span class="crayon-st">
                 IN
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4365d557592274-9">
                <span class="crayon-e">
                 MAC
                </span>
                <span class="crayon-e">
                 uniqueidentifier
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4365d557592274-10">
               </div>
               <div class="crayon-line" id="crayon-5768534d4365d557592274-11">
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 ON
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 [
                </span>
                <span class="crayon-v">
                 PRIMARY
                </span>
                <span class="crayon-sy">
                 ]
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4365d557592274-12">
               </div>
               <div class="crayon-line" id="crayon-5768534d4365d557592274-13">
                <span class="crayon-v">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4365d557592274-14">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                插入测试数据
               </div>
               <div class="crayon-line" id="crayon-5768534d4365d557592274-15">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 TAINFO
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 OID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 Stats
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 MAC
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4365d557592274-16">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 101
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 '46B550F9-6E24-436D-9BC7-F0650F562E54'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 101
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 '46B550F9-6E24-436D-9BC7-F0650F562E54'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 102
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 '46B550F9-6E24-436D-9BC7-F0650F562E54'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4365d557592274-17">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 102
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 '46B550F9-6E24-436D-9BC7-F0650F562E54'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 103
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 '46B550F9-6E24-436D-9BC7-F0650F562E54'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 103
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 '46B550F9-6E24-436D-9BC7-F0650F562E54'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4365d557592274-18">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 103
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 '46B550F9-6E24-436D-9BC7-F0650F562E54'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 103
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 '46B550F9-6E24-436D-9BC7-F0650F562E54'
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4365d557592274-19">
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 GO
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0216 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
       </p>
       <p>
        如果这是你的写的查询语句
       </p>
       <div class="cnblogs_code">
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43664243897749" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           SELECT ID,OID,Stats MAC FROM TAINFO WHERE MAC='46B550F9-6E24-436D-9BC7-F0650F562E54' AND STATS=1  AND OID=102
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43664243897749-1">
                1
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43664243897749-1">
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-v">
                 ID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 OID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-e">
                 Stats
                </span>
                <span class="crayon-e">
                 MAC
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 TAINFO
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-v">
                 MAC
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 '46B550F9-6E24-436D-9BC7-F0650F562E54'
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 AND
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 STATS
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 AND
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 OID
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 102
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0014 seconds] -->
       </div>
       <p>
        我现在根据你的查询语句创建一条索引
       </p>
       <div class="cnblogs_code">
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d4366a563696284" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           CREATE INDEX IX2_TAINFO ON TAINFO(MAC,STATS,OID)
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d4366a563696284-1">
                1
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d4366a563696284-1">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 INDEX
                </span>
                <span class="crayon-e">
                 IX2_TAINFO
                </span>
                <span class="crayon-e">
                 ON
                </span>
                <span class="crayon-e">
                 TAINFO
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 MAC
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 STATS
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 OID
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0054 seconds] -->
       </div>
       <p>
        分别执行三条查询语句
       </p>
       <div class="cnblogs_code">
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d4366f862363151" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           ---1.WHERE条件是索引字段且查询字段也是索引字段
 SELECT ID,OID,Stats MAC FROM TAINFO WHERE MAC='46B550F9-6E24-436D-9BC7-F0650F562E54' AND STATS=1  AND OID=102  
 --2.WHERE 条件是索引的部分字段(这条语句或许是平时查询该表用到的最多的一条语句)
 SELECT ID,OID,Stats MAC FROM TAINFO WHERE OID=102  AND STATS=1
 --3.WHERE 条件是索引的部分字段
  SELECT ID,OID,Stats MAC FROM TAINFO WHERE STATS=1
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d4366f862363151-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4366f862363151-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4366f862363151-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4366f862363151-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4366f862363151-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4366f862363151-6">
                6
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d4366f862363151-1">
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                <span class="crayon-cn">
                 1.WHERE
                </span>
                条件是索引字段且查询字段也是索引字段
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4366f862363151-2">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-v">
                 ID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 OID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-e">
                 Stats
                </span>
                <span class="crayon-e">
                 MAC
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 TAINFO
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-v">
                 MAC
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 '46B550F9-6E24-436D-9BC7-F0650F562E54'
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 AND
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 STATS
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 AND
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 OID
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 102
                </span>
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4366f862363151-3">
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-cn">
                 2.WHERE
                </span>
                <span class="crayon-h">
                </span>
                条件是索引的部分字段
                <span class="crayon-sy">
                 (
                </span>
                这条语句或许是平时查询该表用到的最多的一条语句
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4366f862363151-4">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-v">
                 ID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 OID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-e">
                 Stats
                </span>
                <span class="crayon-e">
                 MAC
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 TAINFO
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-v">
                 OID
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 102
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 AND
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 STATS
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 1
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4366f862363151-5">
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-cn">
                 3.WHERE
                </span>
                <span class="crayon-h">
                </span>
                条件是索引的部分字段
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4366f862363151-6">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-v">
                 ID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 OID
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-e">
                 Stats
                </span>
                <span class="crayon-e">
                 MAC
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 TAINFO
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-v">
                 STATS
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 1
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0121 seconds] -->
       </div>
       <p>
        执行计划分别为
       </p>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/241413348872167.png"/>
       </p>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/241413561684466.png"/>
       </p>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/241414066837186.png"/>
       </p>
       <p>
        从上面三天查询语句可以看出，只有第一条语句走的是索引查找，另外两条语句走的是索引扫描，而我们从字段的名称应该可以看的出OID字段应该是该表的一个外键字段也是经常会被用作查询的字段。
       </p>
       <p>
        接下来我们重新换一下索引顺序
       </p>
       <div class="cnblogs_code">
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43676931498289" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           --创建索引
DROP INDEX IX2_TAINFO ON TAINFO
GO
CREATE INDEX IX1_TAINFO ON TAINFO(OID)
INCLUDE(STATS,MAC)
GO
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43676931498289-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43676931498289-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43676931498289-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43676931498289-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43676931498289-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43676931498289-6">
                6
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43676931498289-1">
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 --
                </span>
                创建索引
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43676931498289-2">
                <span class="crayon-e">
                 DROP
                </span>
                <span class="crayon-e">
                 INDEX
                </span>
                <span class="crayon-e">
                 IX2_TAINFO
                </span>
                <span class="crayon-e">
                 ON
                </span>
                <span class="crayon-e">
                 TAINFO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43676931498289-3">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43676931498289-4">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 INDEX
                </span>
                <span class="crayon-e">
                 IX1_TAINFO
                </span>
                <span class="crayon-e">
                 ON
                </span>
                <span class="crayon-e">
                 TAINFO
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 OID
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43676931498289-5">
                <span class="crayon-e">
                 INCLUDE
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 STATS
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 MAC
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43676931498289-6">
                <span class="crayon-v">
                 GO
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0076 seconds] -->
       </div>
       <p>
        依然执行前面的三条查询语句分析执行计划
       </p>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/241424040748813.png"/>
       </p>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/241424116053279.png"/>
       </p>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/241424187305802.png"/>
       </p>
       <p>
        分析执行计划前面两条查询语句都走的是索引查找，第三条查询的是索引扫描，而根据一般单独用第三条查询的业务应该不会常见，所以现在一条索引解决了两个常用查询的索引需求，避免了建两条索引的必要（所以当你建索引的时候索引的顺序很重要，一般把查询最频繁的字段设第一个字段，可以避免建多余的索引）。
       </p>
       <p>
        为什么要把这个问题提出来呢，因为平时有遇到程序员在写查询语句的时候对于同一个查询条件每次的写法都不一样，往往是根据自己想到哪个字段就写哪个字段先，这样的习惯往往是不好的，就好比上面的例子如果别人看到你的查询条件建一个索引也是这样写的话往往一个表会出现很多多余的索引（或许有人会说DBA建好索引的顺序就好了，这里把这个因素排除吧），像后面的那个索引就解决了两个查询的需求。
       </p>
       <p>
        所以这里我一般是这样规定where条件的，对于经常用作查询的字段放在第一个位置（比如上面例子的OID）,其它的字段根据表的实际字段顺序排列，这样往往你的查询语句走索引的概率会更大。
       </p>
       <p>
       </p>
       <h3>
        理解外连接
       </h3>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d4367d380845148" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           ---创建测试表
IF OBJECT_ID('DBO.OrderS','U') IS NOT NULL DROP TABLE DBO.OrderS
GO
CREATE TABLE DBO.OrderS
(Orderid INT NOT NULL,
custid INT NOT NULL,
stats INT NOT NULL);
GO
IF OBJECT_ID('DBO.Customer','U') IS NOT NULL DROP TABLE DBO.Customer
GO
CREATE TABLE DBO.Customer
(Customerid INT NOT NULL );
GO

---插入测试数据
INSERT INTO OrderS VALUES(1,101,0),(2,102,0),(3,103,1),(4,104,0);
GO
INSERT INTO Customer VALUES(101),(102),(103);


----查询OrderS 表中stats不等于1且不在Customer 表中的数据
SELECT TA.Orderid,TA.custid,TA.stats,TB.Customerid  FROM OrderS TA LEFT JOIN Customer TB ON TA.stats&amp;lt;&amp;gt;'1' AND TA.custid=TB.Customerid 
WHERE TB.Customerid IS NULL
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d4367d380845148-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4367d380845148-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4367d380845148-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4367d380845148-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4367d380845148-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4367d380845148-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4367d380845148-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4367d380845148-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4367d380845148-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4367d380845148-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4367d380845148-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4367d380845148-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4367d380845148-13">
                13
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4367d380845148-14">
                14
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4367d380845148-15">
                15
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4367d380845148-16">
                16
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4367d380845148-17">
                17
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4367d380845148-18">
                18
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4367d380845148-19">
                19
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4367d380845148-20">
                20
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4367d380845148-21">
                21
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4367d380845148-22">
                22
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4367d380845148-23">
                23
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d4367d380845148-1">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                创建测试表
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4367d380845148-2">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 OBJECT_ID
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 'DBO.OrderS'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 'U'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 IS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 DROP
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4367d380845148-3">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4367d380845148-4">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4367d380845148-5">
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 Orderid
                </span>
                <span class="crayon-t">
                 INT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4367d380845148-6">
                <span class="crayon-e">
                 custid
                </span>
                <span class="crayon-t">
                 INT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4367d380845148-7">
                <span class="crayon-e">
                 stats
                </span>
                <span class="crayon-t">
                 INT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4367d380845148-8">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4367d380845148-9">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 OBJECT_ID
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 'DBO.Customer'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 'U'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 IS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 DROP
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customer
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4367d380845148-10">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4367d380845148-11">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-v">
                 DBO
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customer
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4367d380845148-12">
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-t">
                 INT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4367d380845148-13">
                <span class="crayon-v">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4367d380845148-14">
               </div>
               <div class="crayon-line" id="crayon-5768534d4367d380845148-15">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                插入测试数据
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4367d380845148-16">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 101
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 0
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 2
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 102
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 0
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 3
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 103
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 4
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 104
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 0
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4367d380845148-17">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4367d380845148-18">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Customer
                </span>
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 101
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 102
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 103
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4367d380845148-19">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4367d380845148-20">
               </div>
               <div class="crayon-line" id="crayon-5768534d4367d380845148-21">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 --
                </span>
                查询
                <span class="crayon-i">
                 OrderS
                </span>
                <span class="crayon-h">
                </span>
                表中
                <span class="crayon-i">
                 stats
                </span>
                不等于
                <span class="crayon-cn">
                 1
                </span>
                且不在
                <span class="crayon-i">
                 Customer
                </span>
                <span class="crayon-h">
                </span>
                表中的数据
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4367d380845148-22">
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-v">
                 TA
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 Orderid
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 TA
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 custid
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 TA
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 stats
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 TB
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
                <span class="crayon-e">
                 TA
                </span>
                <span class="crayon-e">
                 LEFT
                </span>
                <span class="crayon-e">
                 JOIN
                </span>
                <span class="crayon-e">
                 Customer
                </span>
                <span class="crayon-e">
                 TB
                </span>
                <span class="crayon-e">
                 ON
                </span>
                <span class="crayon-v">
                 TA
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 stats
                </span>
                <span class="crayon-o">
                 &amp;
                </span>
                <span class="crayon-v">
                 lt
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-o">
                 &amp;
                </span>
                <span class="crayon-v">
                 gt
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-s">
                 '1'
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 AND
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 TA
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 custid
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-v">
                 TB
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4367d380845148-23">
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-v">
                 TB
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-st">
                 IS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0240 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/051111244862845.png"/>
       </p>
       <p>
        看到这结果是不是有点疑惑，我在连接条件里面写了TA.stats&lt;&gt;’1’，为什么结果还会查询出。
       </p>
       <p>
        接下来我们换一种写法吧！
       </p>
       <div class="cnblogs_code">
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43684438116697" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           ----查询OrderS 表中stats不等于1且不在Customer 表中的数据
SELECT TA.Orderid,TA.custid,TA.stats,TB.Customerid  FROM OrderS TA LEFT JOIN Customer TB ON  TA.custid=TB.Customerid 
WHERE TA.stats&amp;lt;&amp;gt;'1' AND TB.Customerid IS NULL
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43684438116697-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d43684438116697-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d43684438116697-3">
                3
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43684438116697-1">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 --
                </span>
                查询
                <span class="crayon-i">
                 OrderS
                </span>
                <span class="crayon-h">
                </span>
                表中
                <span class="crayon-i">
                 stats
                </span>
                不等于
                <span class="crayon-cn">
                 1
                </span>
                且不在
                <span class="crayon-i">
                 Customer
                </span>
                <span class="crayon-h">
                </span>
                表中的数据
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d43684438116697-2">
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-v">
                 TA
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 Orderid
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 TA
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 custid
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 TA
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 stats
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 TB
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 OrderS
                </span>
                <span class="crayon-e">
                 TA
                </span>
                <span class="crayon-e">
                 LEFT
                </span>
                <span class="crayon-e">
                 JOIN
                </span>
                <span class="crayon-e">
                 Customer
                </span>
                <span class="crayon-e">
                 TB
                </span>
                <span class="crayon-e">
                 ON
                </span>
                <span class="crayon-v">
                 TA
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 custid
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-v">
                 TB
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d43684438116697-3">
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-v">
                 TA
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 stats
                </span>
                <span class="crayon-o">
                 &amp;
                </span>
                <span class="crayon-v">
                 lt
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-o">
                 &amp;
                </span>
                <span class="crayon-v">
                 gt
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-s">
                 '1'
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 AND
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 TB
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 Customerid
                </span>
                <span class="crayon-st">
                 IS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0029 seconds] -->
       </div>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/051115120498890.png"/>
       </p>
       <p>
        接下来我就解释一下原因：对于外连接，连接条件不会改变主表的数据，即不会删减主表的数据
       </p>
       <p>
        对于上面的查询主表是orders，所以无论你在连接条件on里面怎样设置主表的条件都不影响主表数据的输出，影响主表数据的输出只在where条件里，where条件影响最后数据的输出。而对于附表Customer 的条件就应该写在连接条件（on）里而不是where条件里，这里说的是外连接（包括左连接和右连接）。
       </p>
       <p>
        对于inner join就不存在这种情况，无论你的条件是写在where后面还是on后面都是一样的，但是还是建议写在where后面。
       </p>
       <h3>
        谓词类型要与字段类型对齐
       </h3>
       <div class="cnblogs_code">
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
        <p>
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d4368b667906527" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           IF OBJECT_ID('Person','u')IS NOT NULL DROP TABLE Person
GO
CREATE TABLE Person
(ID INT NOT NULL PRIMARY KEY IDENTITY(1,1),
Phone NVARCHAR(20) NOT NULL,
CreateDate DATETIME NOT NULL
)
---插入测试数据
INSERT INTO Person(Phone,CreateDate)
VALUES('13700000000',GETDATE()),('13700000000',GETDATE()),('13800000000',GETDATE())

---创建索引
CREATE INDEX IX_Person ON Person(Phone,CreateDate)
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d4368b667906527-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4368b667906527-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4368b667906527-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4368b667906527-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4368b667906527-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4368b667906527-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4368b667906527-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4368b667906527-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4368b667906527-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4368b667906527-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4368b667906527-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4368b667906527-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4368b667906527-13">
                13
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d4368b667906527-1">
                <span class="crayon-st">
                 IF
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 OBJECT_ID
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 'Person'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-s">
                 'u'
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-st">
                 IS
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 DROP
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-e">
                 Person
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4368b667906527-2">
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4368b667906527-3">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 TABLE
                </span>
                <span class="crayon-e">
                 Person
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4368b667906527-4">
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-e">
                 ID
                </span>
                <span class="crayon-t">
                 INT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 PRIMARY
                </span>
                <span class="crayon-e">
                 KEY
                </span>
                <span class="crayon-e">
                 IDENTITY
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4368b667906527-5">
                <span class="crayon-e">
                 Phone
                </span>
                <span class="crayon-e">
                 NVARCHAR
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-cn">
                 20
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
                <span class="crayon-sy">
                 ,
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4368b667906527-6">
                <span class="crayon-e">
                 CreateDate
                </span>
                <span class="crayon-e">
                 DATETIME
                </span>
                <span class="crayon-st">
                 NOT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 NULL
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4368b667906527-7">
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4368b667906527-8">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                插入测试数据
               </div>
               <div class="crayon-line" id="crayon-5768534d4368b667906527-9">
                <span class="crayon-e">
                 INSERT
                </span>
                <span class="crayon-e">
                 INTO
                </span>
                <span class="crayon-e">
                 Person
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 Phone
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 CreateDate
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4368b667906527-10">
                <span class="crayon-e">
                 VALUES
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 '13700000000'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-e">
                 GETDATE
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 '13700000000'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-e">
                 GETDATE
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 '13800000000'
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-e">
                 GETDATE
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4368b667906527-11">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4368b667906527-12">
                <span class="crayon-o">
                 --
                </span>
                <span class="crayon-o">
                 -
                </span>
                创建索引
               </div>
               <div class="crayon-line" id="crayon-5768534d4368b667906527-13">
                <span class="crayon-e">
                 CREATE
                </span>
                <span class="crayon-e">
                 INDEX
                </span>
                <span class="crayon-e">
                 IX_Person
                </span>
                <span class="crayon-e">
                 ON
                </span>
                <span class="crayon-e">
                 Person
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 Phone
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 CreateDate
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0112 seconds] -->
        <p>
        </p>
        <div class="cnblogs_code_toolbar">
         <span class="cnblogs_code_copy">
          <p title="复制代码">
           <img src="http://dataunion.org/wp-content/uploads/2015/05/copycode.gif"/>
          </p>
         </span>
        </div>
       </div>
       <p>
        1.谓词类型与字段类型不一致
       </p>
       <div class="cnblogs_code">
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43692585090445" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           SELECT ID FROM Person WHERE Phone=13700000000 AND DATEDIFF(DAY,CreateDate,GETDATE())=0
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43692585090445-1">
                1
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43692585090445-1">
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-e">
                 ID
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 Person
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-v">
                 Phone
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 13700000000
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 AND
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 DATEDIFF
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 DAY
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 CreateDate
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-e">
                 GETDATE
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 0
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0013 seconds] -->
       </div>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/131544156637810.png"/>
       </p>
       <p>
        由于定义表的phone字段类型是字符型，而上面的查询条件phone写成了整形，导致执行计划走了索引扫描，且执行计划select也有提示。
       </p>
       <p>
        2.谓词类型与字段类型一致
       </p>
       <div class="cnblogs_code">
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d43697466080336" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           SELECT ID FROM Person WHERE Phone='13700000000' AND DATEDIFF(DAY,CreateDate,GETDATE())=0
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d43697466080336-1">
                1
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d43697466080336-1">
                <span class="crayon-e">
                 SELECT
                </span>
                <span class="crayon-e">
                 ID
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-e">
                 Person
                </span>
                <span class="crayon-e">
                 WHERE
                </span>
                <span class="crayon-v">
                 Phone
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 '13700000000'
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 AND
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 DATEDIFF
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 DAY
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 CreateDate
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-e">
                 GETDATE
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-cn">
                 0
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0012 seconds] -->
       </div>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/131545088035978.png"/>
       </p>
       <p>
        第二种查询phone谓词类型与字段类型一致，所以查询走了索引查找
       </p>
       <p>
        在日常的语句编写过程中需要注意这类问题，这将直接影响性能。
       </p>
       <p>
        <strong>
         避免使用长字节字段排序
        </strong>
       </p>
       <div class="cnblogs_code">
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-5768534d4369d929787423" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           SELECT O.name,O.create_date,C.name  FROM SYS.columns C INNER JOIN SYS.objects  O ON C.object_id=O.object_id 
 ORDER BY O.create_date DESC
 GO
 SELECT O.name,O.create_date,C.name  FROM SYS.columns C INNER JOIN SYS.objects  O ON C.object_id=O.object_id 
 ORDER BY O.object_id  DESC
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-5768534d4369d929787423-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4369d929787423-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4369d929787423-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-5768534d4369d929787423-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-5768534d4369d929787423-5">
                5
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-5768534d4369d929787423-1">
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 SELECT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 O
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 name
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 O
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 create_date
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 C
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 name
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-v">
                 SYS
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-i">
                 columns
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 C
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 INNER
                </span>
                <span class="crayon-e">
                 JOIN
                </span>
                <span class="crayon-v">
                 SYS
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-i">
                 objects
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 O
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 ON
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 C
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 object_id
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-v">
                 O
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 object_id
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4369d929787423-2">
                <span class="crayon-e">
                </span>
                <span class="crayon-e">
                 ORDER
                </span>
                <span class="crayon-i">
                 BY
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 O
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 create_date
                </span>
                <span class="crayon-e">
                 DESC
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4369d929787423-3">
                <span class="crayon-e">
                </span>
                <span class="crayon-e">
                 GO
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-5768534d4369d929787423-4">
                <span class="crayon-e">
                </span>
                <span class="crayon-i">
                 SELECT
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 O
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 name
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 O
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 create_date
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-v">
                 C
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 name
                </span>
                <span class="crayon-e">
                 FROM
                </span>
                <span class="crayon-v">
                 SYS
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-i">
                 columns
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 C
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 INNER
                </span>
                <span class="crayon-e">
                 JOIN
                </span>
                <span class="crayon-v">
                 SYS
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-i">
                 objects
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 O
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 ON
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 C
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 object_id
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-v">
                 O
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 object_id
                </span>
               </div>
               <div class="crayon-line" id="crayon-5768534d4369d929787423-5">
                <span class="crayon-e">
                </span>
                <span class="crayon-e">
                 ORDER
                </span>
                <span class="crayon-i">
                 BY
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 O
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 object_id
                </span>
                <span class="crayon-v">
                 DESC
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0042 seconds] -->
       </div>
       <p>
       </p>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/121020041058690.png"/>
       </p>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/05/121020162143555.png"/>
       </p>
       <p>
        上面的语句查询结果是一样的，只是写法不一样，O.create_date是表的创建时间而object_id 是一个自增值根据两者的倒序排序得到的结果是一样的，但是二者的执行效率却不一样。无论是从执行时间还是执行计划明显是后者的效率要好，从执行计划可以看出后者的不需要进行排序操作因为object_id 本身就是排序好的，而且object_id 是整形而create_date是时间类型，如果是两个大表进行连接操作再进行排序效率更明显甚至前面用时间排序还可能查询很久不出来。
       </p>
       <p>
        原文作者：pursuer.chen
       </p>
       <p>
        原文地址：
        <a href="http://www.cnblogs.com/chenmh/p/3999475.html">
         http://www.cnblogs.com/chenmh/p/3999475.html
        </a>
       </p>
      </div>
      <div>
       <strong>
        注：转载文章均来自于公开网络，仅供学习使用，不会用于任何商业用途，如果侵犯到原作者的权益，请您与我们联系删除或者授权事宜，联系邮箱：contact@dataunion.org。转载数盟网站文章请注明原文章作者，否则产生的任何版权纠纷与数盟无关。
       </strong>
      </div>
      <!--content_text-->
      <div class="fenxian">
       <!-- JiaThis Button BEGIN -->
       <div class="jiathis_style_32x32">
        <p class="jiathis_button_weixin">
        </p>
        <p class="jiathis_button_tsina">
        </p>
        <p class="jiathis_button_qzone">
        </p>
        <p class="jiathis_button_cqq">
        </p>
        <p class="jiathis_button_tumblr">
        </p>
        <a class="jiathis jiathis_txt jtico jtico_jiathis" href="http://www.jiathis.com/share" target="_blank">
        </a>
        <p class="jiathis_counter_style">
        </p>
       </div>
       <!-- JiaThis Button END -->
      </div>
     </article>
     <!--content-->
     <!--相关文章-->
     <div class="xianguan">
      <div class="xianguantitle">
       相关文章！
      </div>
      <ul class="pic">
       <li>
        <a href="http://dataunion.org/20037.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/07/t01cbbd7ae2c54cab40.png"/>
        </a>
        <a class="link" href="http://dataunion.org/20037.html" rel="bookmark" title="记一次苦逼的SQL查询优化">
         记一次苦逼的SQL查询优化
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/20021.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/07/1c191_副本.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20021.html" rel="bookmark" title="关于时间序列数据库的一点思考">
         关于时间序列数据库的一点思考
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/19122.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/06/t01c8e61852c2874d06-300x225.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/19122.html" rel="bookmark" title="淘宝数据库OceanBase SQL编译器源码阅读：生成逻辑计划">
         淘宝数据库OceanBase SQL编译器源码阅读：生成逻辑计划
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/17652.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/05/QQ截图20150530222020.png"/>
        </a>
        <a class="link" href="http://dataunion.org/17652.html" rel="bookmark" title="QueryBuilder ： 打造优雅的Linq To SQL动态查询">
         QueryBuilder ： 打造优雅的Linq To SQL动态查询
        </a>
       </li>
      </ul>
     </div>
     <!--相关文章-->
     <div class="comment" id="comments">
      <!-- You can start editing here. -->
      <!-- If comments are open, but there are no comments. -->
      <div class="title">
       期待你一针见血的评论，Come on！
      </div>
      <div id="respond">
       <p>
        不用想啦，马上
        <a href="http://dataunion.org/wp-login.php?redirect_to=http%3A%2F%2Fdataunion.org%2F17664.html">
         "登录"
        </a>
        发表自已的想法.
       </p>
      </div>
     </div>
     <!-- .nav-single -->
    </div>
    <!--Container End-->
    <aside id="sitebar">
     <div class="sitebar_list2">
      <div class="wptag">
       <span class="tagtitle">
        热门标签+
       </span>
       <div class="tagg">
        <ul class="menu" id="menu-%e5%8f%8b%e6%83%85%e9%93%be%e6%8e%a5">
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1605" id="menu-item-1605">
          <a href="http://taidizh.com/">
           泰迪智慧
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20884" id="menu-item-20884">
          <a href="http://www.transwarp.cn/">
           星环科技
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-3538" id="menu-item-3538">
          <a href="http://datall.org/">
           珈和遥感
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20888" id="menu-item-20888">
          <a href="http://www.chinahadoop.cn/">
           小象学院
          </a>
         </li>
        </ul>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <div class="textwidget">
       <div align="center">
        <a href="http://study.163.com/course/courseMain.htm?courseId=991022" target="_blank">
         <img src="http://dataunion.org/wp-content/uploads/2016/03/dv.jpg"/>
        </a>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       文章分类
      </h4>
      <div class="tagcloud">
       <a class="tag-link-44" href="http://dataunion.org/category/industry/demo" style="font-size: 10.204724409449pt;" title="4个话题">
        Demo展示
       </a>
       <a class="tag-link-31" href="http://dataunion.org/category/experts" style="font-size: 15.826771653543pt;" title="52个话题">
        专家团队
       </a>
       <a class="tag-link-870" href="http://dataunion.org/category/tech/ai" style="font-size: 19.795275590551pt;" title="273个话题">
        人工智能
       </a>
       <a class="tag-link-488" href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f" style="font-size: 8pt;" title="1个话题">
        加入数盟
       </a>
       <a class="tag-link-869" href="http://dataunion.org/category/tech/viz" style="font-size: 17.204724409449pt;" title="93个话题">
        可视化
       </a>
       <a class="tag-link-30" href="http://dataunion.org/category/partners" style="font-size: 10.645669291339pt;" title="5个话题">
        合作伙伴
       </a>
       <a class="tag-link-889" href="http://dataunion.org/category/parterc" style="font-size: 11.582677165354pt;" title="8个话题">
        合作会议
       </a>
       <a class="tag-link-104" href="http://dataunion.org/category/books" style="font-size: 12.96062992126pt;" title="15个话题">
        图书
       </a>
       <a class="tag-link-220" href="http://dataunion.org/category/tech/base" style="font-size: 19.850393700787pt;" title="281个话题">
        基础架构
       </a>
       <a class="tag-link-219" href="http://dataunion.org/category/tech/analysis" style="font-size: 19.409448818898pt;" title="232个话题">
        数据分析
       </a>
       <a class="tag-link-887" href="http://dataunion.org/category/tech/dm" style="font-size: 13.291338582677pt;" title="17个话题">
        数据挖掘
       </a>
       <a class="tag-link-34" href="http://dataunion.org/category/tech" style="font-size: 20.732283464567pt;" title="404个话题">
        文章
       </a>
       <a class="tag-link-1" href="http://dataunion.org/category/uncategorized" style="font-size: 22pt;" title="693个话题">
        未分类
       </a>
       <a class="tag-link-4" href="http://dataunion.org/category/events" style="font-size: 14.503937007874pt;" title="29个话题">
        活动
       </a>
       <a class="tag-link-890" href="http://dataunion.org/category/tech/%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0" style="font-size: 10.204724409449pt;" title="4个话题">
        深度学习
       </a>
       <a class="tag-link-221" href="http://dataunion.org/category/tech/devl" style="font-size: 18.968503937008pt;" title="193个话题">
        编程语言
       </a>
       <a class="tag-link-888" href="http://dataunion.org/category/career" style="font-size: 15.661417322835pt;" title="48个话题">
        职业规划
       </a>
       <a class="tag-link-5" href="http://dataunion.org/category/jobs" style="font-size: 14.11811023622pt;" title="25个话题">
        职位
       </a>
       <a class="tag-link-871" href="http://dataunion.org/category/industry" style="font-size: 15.716535433071pt;" title="49个话题">
        行业
       </a>
       <a class="tag-link-613" href="http://dataunion.org/category/industry/case" style="font-size: 16.984251968504pt;" title="84个话题">
        行业应用
       </a>
       <a class="tag-link-885" href="http://dataunion.org/category/industry/news" style="font-size: 17.425196850394pt;" title="102个话题">
        行业资讯
       </a>
       <a class="tag-link-10" href="http://dataunion.org/category/training" style="font-size: 14.228346456693pt;" title="26个话题">
        课程
       </a>
       <a class="tag-link-16" href="http://dataunion.org/category/sources" style="font-size: 15.661417322835pt;" title="48个话题">
        资源
       </a>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       功能
      </h4>
      <ul>
       <li>
        <a href="http://dataunion.org/wp-login.php?action=register">
         注册
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/wp-login.php">
         登录
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/feed">
         文章
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/comments/feed">
         评论
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="https://cn.wordpress.org/" title="基于WordPress，一个优美、先进的个人信息发布平台。">
         WordPress.org
        </a>
       </li>
      </ul>
     </div>
    </aside>
    <div class="clear">
    </div>
   </div>
   <!--main-->
   ﻿
   <footer id="dibu">
    <div class="about">
     <div class="right">
      <ul class="menu" id="menu-%e5%ba%95%e9%83%a8%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-18024" id="menu-item-18024">
        <a href="http://dataunion.org/category/partners">
         合作伙伴
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20881" id="menu-item-20881">
        <a href="http://dataunion.org/contribute">
         文章投稿
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20872" id="menu-item-20872">
        <a href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f">
         加入数盟
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22441" id="menu-item-22441">
        <a href="http://dataunion.org/f-links">
         友情链接
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20874" id="menu-item-20874">
        <a href="http://dataunion.org/aboutus">
         关于数盟
        </a>
       </li>
      </ul>
      <p class="banquan">
       数盟社区        ，
        做最棒的数据科学社区
      </p>
     </div>
     <div class="left">
      <ul class="bottomlist">
       <li>
        <a href="http://weibo.com/DataScientistUnion  " target="_blank" 　title="">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weibo.png"/>
        </a>
       </li>
       <li>
        <a class="cd-popup-trigger" href="http://dataunion.org/17664.html#0">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weixin.png"/>
        </a>
       </li>
      </ul>
      <div class="cd-popup">
       <div class="cd-popup-container">
        <h1>
         扫描二维码,加微信公众号
        </h1>
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/2014-12-06-1515289049.png"/>
        <a class="cd-popup-close" href="http://dataunion.org/17664.html">
        </a>
       </div>
       <!-- cd-popup-container -->
      </div>
      <!-- cd-popup -->
     </div>
    </div>
    <!--about-->
    <div class="bottom">
     <a href="http://dataunion.org/">
      数盟社区
     </a>
     <a href="http://www.miitbeian.gov.cn/" rel="external nofollow" target="_blank">
      京ICP备14026740号
     </a>
     联系我们：
     <a href="mailto:contact@dataunion.org" target="_blank">
      contact@dataunion.org
     </a>
     <div class="tongji">
     </div>
     <!--bottom-->
     <div class="scroll" id="scroll" style="display:none;">
      ︿
     </div>
    </div>
   </footer>
   <!--dibu-->
  </div>
 </body>
</html>